Skip to main content

A simple nginx config and control module

Project description

PySimpleNginx

这是一个简单的Python程序,用于构建一个Nginx配置文件和控制Nginx。

安装

(1)使用git安装

$ git clone https://github.com/stemsky/PySimpleNginx.git
$ cd PySimpleNginx
$ python setup.py install

(2)使用pip安装

$ pip install PySimpleNginx

使用

(1)创建一个配置文件

from PyNginx import config
conf = config.Config()

你还可以从字符串创建一个配置文件:

conf = config.Config('user www www;\nworker_processes 1;\nevents ...')

(2)添加一个全局块

block = config.global_block()
block.key = 'user'
block.value = ['www', 'www']
conf.value.append(block)

同样,你也可以从字符串创建一个全局块:

block = config.global_block('user www www;')
conf.value.append(block)

(3)添加一个events块

events = config.events_block()
block = config.events_global_block()
block.key = 'worker_connections'
block.value = '1024'
events.value.append(block)
conf.value.append(events)

其实,对于events中的块,你可以直接使用global_block()类创建,但是这样做会更加优雅。

(4)添加添加其它块

其它块的操作方式和以上方式一样,但要注意的是http中的全局块必须使用http_global_block()类创建,而不是global_block()类。具体原因可以查看源码。

(5)保存配置文件

先将Config对象转换成字符串,然后保存到文件中。

with open('nginx.conf', 'w') as f:
    f.write(str(conf))

(6)控制Nginx

另一个文件control.py,用于控制Nginx。但它只能用于Linux系统,不能在Windows上运行,否则会报错OSError。

(1)启动Nginx

from pynginx.control import *
start()

这一步要求你必须安装了Nginx,并且配置了环境变量。

(2)停止Nginx

# 从容地关闭Nginx(结束所有服务后再关闭)
stop('clam')
# 立刻关闭Nginx
stop('quickly')
# 强制关闭Nginx
stop('force')
# 默认是从容地关闭Nginx(结束所有服务后再关闭)
stop()

(3)重启Nginx

# 简单地重启Nginx(这里的简单指先关闭再启动)
restart('easy')
# 重载Nginx配置文件(不关闭Nginx,也不停止请求,只是重新加载配置文件)
restart('reload')
# 还可以指定配置文件的路径,默认是/usr/local/nginx/conf/nginx.conf
restart('reload', '/usr/local/nginx/conf/nginx.conf')
# 默认为重载Nginx配置文件
restart()

(4)测试Nginx配置文件

# 测试Nginx配置文件,如果配置文件有误,会返回False
test('/usr/local/nginx/conf/nginx.conf')
# 里面的一个参数是配置文件的路径,默认是/usr/local/nginx/conf/nginx.conf
test()

缺点

(1)对于配置文件格式不是很规范的数据,可以解析时会出错。我没有严谨地测试过。

(2)config中的类名除Config外都是小写。因为我并没有大写的习惯,所以这样写了。

(3)该程序的鲁棒性不是很好。

(4)控制Nginx的程序只能在Linux系统上运行。

……


总之,该程序的缺点有很多。因为这是作者第一次开发库,所以还有很多地方没有完善。如果你遇到问题请不要吐槽,可以issue给我。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PySimpleNginx-0.2.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PySimpleNginx-0.2.1-py3.7.egg (15.7 kB view details)

Uploaded Egg

File details

Details for the file PySimpleNginx-0.2.1.tar.gz.

File metadata

  • Download URL: PySimpleNginx-0.2.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for PySimpleNginx-0.2.1.tar.gz
Algorithm Hash digest
SHA256 569ca4592537b697b2627c4f232f179c2d56d336036956b9ca81708cafef0511
MD5 55235a9c5fe8509e13228c54e2ffe8b9
BLAKE2b-256 47fc172d8c4fb557ef5400e5b4d79f27575ac0227f7e178514d92bb3a8a4594b

See more details on using hashes here.

File details

Details for the file PySimpleNginx-0.2.1-py3.7.egg.

File metadata

  • Download URL: PySimpleNginx-0.2.1-py3.7.egg
  • Upload date:
  • Size: 15.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for PySimpleNginx-0.2.1-py3.7.egg
Algorithm Hash digest
SHA256 c9b7f05665063e5d76fc633883f665492690ec1a8de3acfddee36ab7bc988caa
MD5 64423fad6d304ddbdaf1579aeb1ed8ca
BLAKE2b-256 09cbdd6716c7dd3fa23e9f6f202ec223ba33c1120c6b4695967a816f51807bec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page