I have a python script that dynamically alters nginx config file (nginx.conf). Since nginx configuration is not in ini format, i currently use some regexp to parse and modify file content. Is it the only way or some better way to programmatically alter nginx configuration exist?
-
3Why not use a template to generate the file?Martijn Pieters– Martijn Pieters2013-03-07 17:16:54 +00:00Commented Mar 7, 2013 at 17:16
-
2@Mart I don't generate it - i alter existing one. Script is used on existing servers that already has nginx up and running with existing config files. I add or remove items depending on some logic.grigoryvp– grigoryvp2013-03-07 17:18:27 +00:00Commented Mar 7, 2013 at 17:18
5 Answers
NGINX Inc. started providing a library in 2017 that is a "quick and reliable way to convert NGINX configurations into JSON and back." It's called crossplane, and you can get it with pip.
pip install crossplane
With this library, you can parse a file to JSON, deserialize, manipulate, serialize back to JSON, and then convert back to the NGINX config file format.
Comments
You might use the PyNginxConfig Parser. http://code.google.com/p/pynginxconfig/source/browse/pynginxconfig.py#208 This implementation is close to the original one.
There is also ServerMgr. It's not really a parser, just allows for basic mappings.
Last but not least, actually first, because based on pyparsing: https://github.com/pyhedgehog/nginxparser
pip install git+https://github.com/pyhedgehog/nginxparser.git
2 Comments
pip install git+https://github.com/fatiherikli/nginxparser.git There seems to exist an augeas lens supporting at least a subset of the nginx config file syntax. Augeas also has Python bindings.
2 Comments
Just found out about python-nginx, which works great out-of-the-box using only Python, and doesn't seem to need any C or required Python package at all! Could improve docs a bit. Maybe I'll send a pull request for that.
Comments
There is now nginx-conf, an NPM module that provides programmatic access to nginx configurations.