Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/peakwinter/python-nginx
Create and modify nginx serverblock configs in Python
https://github.com/peakwinter/python-nginx
nginx nginx-server nginx-serverblock python python-2 python-library python-nginx python3
Last synced: 22 days ago
JSON representation
Create and modify nginx serverblock configs in Python
- Host: GitHub
- URL: https://github.com/peakwinter/python-nginx
- Owner: peakwinter
- License: gpl-3.0
- Created: 2013-11-24T04:32:12.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2023-02-22T01:31:22.000Z (over 1 year ago)
- Last Synced: 2024-10-13T09:03:46.620Z (about 1 month ago)
- Topics: nginx, nginx-server, nginx-serverblock, python, python-2, python-library, python-nginx, python3
- Language: Python
- Size: 130 KB
- Stars: 296
- Watchers: 21
- Forks: 79
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
## python-nginx
![build](https://github.com/peakwinter/python-nginx/actions/workflows/ci.yml/badge.svg)
A module for easily creating and modifying nginx serverblock configurations in Python (including comments!).
### Install
pip install python-nginx
### Examples
Create an nginx serverblock and save it to file:
>>> import nginx
>>> c = nginx.Conf()
>>> u = nginx.Upstream('php',
... nginx.Key('server', 'unix:/tmp/php-fcgi.socket')
... )
>>> c.add(u)
>>> s = nginx.Server()
>>> s.add(
... nginx.Key('listen', '80'),
... nginx.Comment('Yes, python-nginx can read/write comments!'),
... nginx.Key('server_name', 'localhost 127.0.0.1'),
... nginx.Key('root', '/srv/http'),
... nginx.Key('index', 'index.php'),
... nginx.Location('= /robots.txt',
... nginx.Key('allow', 'all'),
... nginx.Key('log_not_found', 'off'),
... nginx.Key('access_log', 'off')
... ),
... nginx.Location('~ \.php$',
... nginx.Key('include', 'fastcgi.conf'),
... nginx.Key('fastcgi_intercept_errors', 'on'),
... nginx.Key('fastcgi_pass', 'php')
... )
... )
>>> c.add(s)
>>> nginx.dumpf(c, '/etc/nginx/sites-available/mysite')Load an nginx serverblock from a file:
>>> import nginx
>>> c = nginx.loadf('/etc/nginx/sites-available/testsite')
>>> c.children
[]
>>> c.server.children
[, , , ]
>>> c.as_dict
{'conf': [{'server': [{'#': 'This is a test comment'}, {'server_name': 'localhost'}, {'root': '/srv/http'}, {'location /': [{'allow': 'all'}]}]}]}Format an nginx serverblock into a string (change the amount of spaces (or tabs) for each indentation level by modifying `nginx.INDENT` first):
>>> c.servers
[]
>>> c.as_strings
['server {\n', ' # This is a test comment\n', ' server_name localhost;\n', ' root /srv/http;\n', '\n location / {\n', ' allow all;\n', ' }\n\n', '}\n']Find where you put your keys:
>>> import nginx
>>> c = nginx.loadf('/etc/nginx/sites-available/testsite')
>>> c.filter('Server')
[]
>>> c.filter('Server')[0].filter('Key', 'root')
[]
>>> c.filter('Server')[0].filter('Location')
[]Or just get everything by its type:
>>> import nginx
>>> c = nginx.loadf('/etc/nginx/sites-available/testsite')
>>> c.servers
[]
>>> c.servers[0].keys
[, ]