Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any good way to programmatically change nginx config file from python?

Tags:

python

nginx

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?

like image 391
grigoryvp Avatar asked Mar 07 '13 17:03

grigoryvp


People also ask

What method could you use to make dynamic configuration changes with Nginx plus?

NGINX Plus adds additional methods for modifying configuration dynamically. By using DNS as the definitive repository for upstream servers, you can make changes without touching NGINX Plus configuration files.

What language are Nginx config files?

The NGINX source code is written in the C programming language by Kernighan and Ritchie and maintains a consistent style.


2 Answers

  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/fatiherikli/nginxparser

    pip install git+https://github.com/fatiherikli/nginxparser.git

like image 186
Jens A. Koch Avatar answered Oct 18 '22 23:10

Jens A. Koch


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 crossplan and you can get it with pip.

pip install crossplane

With this you can parse a file to JSON, deserialize, manipulate, serialize back to JSON, and then convert back to the NGINX config file format.

like image 22
Jeremy Cook Avatar answered Oct 18 '22 23:10

Jeremy Cook