|
2 | 2 |
|
3 | 3 | A module for easily creating and modifying nginx serverblock configurations in Python (including comments!). |
4 | 4 |
|
5 | | -Work in progress. |
| 5 | +### Examples |
| 6 | + |
| 7 | +Create an nginx serverblock and save it to file: |
| 8 | + |
| 9 | + >>> import nginx |
| 10 | + >>> c = nginx.Conf() |
| 11 | + >>> u = nginx.Upstream('php', |
| 12 | + ... nginx.Key('server', 'unix:/tmp/php-fcgi.socket') |
| 13 | + ... ) |
| 14 | + >>> c.add(u) |
| 15 | + >>> s = nginx.Server() |
| 16 | + >>> s.add( |
| 17 | + ... nginx.Key('listen', '80'), |
| 18 | + ... nginx.Comment('Yes, python-nginx can read/write comments!'), |
| 19 | + ... nginx.Key('server_name', 'localhost 127.0.0.1'), |
| 20 | + ... nginx.Key('root', '/srv/http'), |
| 21 | + ... nginx.Key('index', 'index.php'), |
| 22 | + ... nginx.Location('= /robots.txt', |
| 23 | + ... nginx.Key('allow', 'all'), |
| 24 | + ... nginx.Key('log_not_found', 'off'), |
| 25 | + ... nginx.Key('access_log', 'off') |
| 26 | + ... ) |
| 27 | + ... nginx.Location('~ \.php$', |
| 28 | + ... nginx.Key('include', 'fastcgi.conf'), |
| 29 | + ... nginx.Key('fastcgi_intercept_errors', 'on'), |
| 30 | + ... nginx.Key('fastcgi_pass', 'php') |
| 31 | + ... ) |
| 32 | + ... ) |
| 33 | + >>> c.add(s) |
| 34 | + >>> nginx.dumpf(c, '/etc/nginx/sites-available/mysite') |
| 35 | + |
| 36 | +Load an nginx serverblock from a file: |
| 37 | + |
| 38 | + >>> import nginx |
| 39 | + >>> c = nginx.loadf('/etc/nginx/sites-available/testsite') |
| 40 | + >>> c.all() |
| 41 | + [<main.Server object at 0x7f1ed4573890>] |
| 42 | + >>> c.servers[0].all() |
| 43 | + [<main.Comment object at 0x7f1ed45736d0>, <main.Key object at 0x7f1ed4573750>, <main.Key object at 0x7f1ed4573790>, <main.Location object at 0x7f1ed4573850>] |
| 44 | + >>> c.as_dict() |
| 45 | + {'conf': [{'server': [{'#': 'This is a test comment'}, {'server_name': 'localhost'}, {'root': '/srv/http'}, {'location /': [{'allow': 'all'}]}]}]} |
| 46 | + |
| 47 | +Format an nginx serverblock into a string (change the amount of spaces (or tabs) for each indentation level by modifying `nginx.INDENT` first): |
| 48 | + |
| 49 | + >>> c.all() |
| 50 | + [<main.Server object at 0x7f1ed4573890>] |
| 51 | + >>> c.as_block() |
| 52 | + ['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'] |
| 53 | + |
| 54 | +Find where you put your keys: |
| 55 | + |
| 56 | + >>> import nginx |
| 57 | + >>> c = nginx.loadf('/etc/nginx/sites-available/testsite') |
| 58 | + >>> c.filter('Server') |
| 59 | + [<main.Server object at 0x7f1ed4573890>] |
| 60 | + >>> c.filter('Server')[0].filter('Key', 'root') |
| 61 | + [<main.Key object at 0x7f1ed4573790>] |
| 62 | + >>> c.filter('Server')[0].filter('Location') |
| 63 | + [<main.Location object at 0x7f1ed4573850>] |
| 64 | + |
| 65 | +Or just get everything by its type: |
| 66 | + |
| 67 | + >>> import nginx |
| 68 | + >>> c = nginx.loadf('/etc/nginx/sites-available/testsite') |
| 69 | + >>> c.servers |
| 70 | + [<main.Server object at 0x7f1ed4573890>] |
| 71 | + >>> c.servers[0].keys |
| 72 | + [<main.Key object at 0x7f1ed4573750>, <main.Key object at 0x7f1ed4573790>] |
0 commit comments