25

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?

2
  • 3
    Why not use a template to generate the file? Commented 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. Commented Mar 7, 2013 at 17:18

5 Answers 5

12

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.

Sign up to request clarification or add additional context in comments.

Comments

11
  1. https://www.npmjs.com/package/nginx-conf

  2. 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.

  3. There is also ServerMgr. It's not really a parser, just allows for basic mappings.

  4. 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

Install nginxparser with pip install git+https://github.com/fatiherikli/nginxparser.git
The gihub.com/fatiherikli account appears to have changed or closed. While unsure what would be the best fork to suggest, this one at least has a Credits section stating it was forked from the original repository: github.com/pyhedgehog/nginxparser
4

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

Looks kinda overkill to use library written in C with python bindings for simple text file processing O_O.
For anyone looking to parse nginx.conf files from languages other than Python Augeas also has bindings for a number of other langauges, in addition to a shell tool. Also my 2 cents - Augeas is the most robust solution as it provides a common API to parse many .conf files from different languages. The nginx lens is currently restricted to a subset of nginx configuration syntax but it's fairly straightforward to extend.
3

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

-2

There is now nginx-conf, an NPM module that provides programmatic access to nginx configurations.

2 Comments

The OP asked about Python -- why is the accepted answer one suggesting an NPM (Javascript) module?
Answer provides for JavaScript, not Python as the author requested. Downvoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.