linkedinattic / nginx-config-builder Goto Github PK
View Code? Open in Web Editor NEWA python library for building nginx configuration files programatically
License: BSD 2-Clause "Simplified" License
A python library for building nginx configuration files programatically
License: BSD 2-Clause "Simplified" License
Hi,
I came across this library a couple of days ago and it's really helpful. However, I've got into an issue with the library not maintaining the order of config blocks. Some nginx configs are sensitive to their position in file. for e.g. 'log_format' that I use should be defined before it's used in access_log.
Any suggestion how to approach such issue?
Thanks
In [1]: from nginx.config.api import Config, Section, Location, KeyOption, KeyValueOption, KeyMultiValueOption
In [2]: http = Section('http', include='../conf/mime.types')
In [3]: server1 = Section('server', KeyValueOption('listen', '80'), KeyValueOption('server_name', 'server1.com'), KeyMultiValueOption('return', [301, 'https://$server_name$request_uri']))
In [4]: server2 = Section('server', KeyValueOption('listen', '443'), KeyValueOption('server_name', 'server2.com'), KeyMultiValueOption('return', [302, 'https://$server_name$request_uri']))
In [5]: http.sections.add(server1, server2)
In [6]: http
Out[6]:
http {
include ../conf/mime.types;
server {
listen 443;
server_name server2.com;
return 302 https://$server_name$request_uri;
}
}
I then make a very small change to the code and run it again:
sidler@76 ~ cd repos/scottidler/nginx-config-builder/src
sidler@76 ~/repos/scottidler/nginx-config-builder/src master ● git diff
diff --git i/src/nginx/config/api/options.py w/src/nginx/config/api/options.py
index 18f8945..0b6db52 100644
--- i/src/nginx/config/api/options.py
+++ w/src/nginx/config/api/options.py
@@ -116,7 +116,6 @@ class AttrDict(dict):
self._owner = owner
return ret
-
class AttrList(AttrDict):
""" A dictionary/list hybrid that exposes values as attributes. """
def __iter__(self):
@@ -125,7 +124,7 @@ class AttrList(AttrDict):
def append(self, item):
if hasattr(item, '_parent'):
item._parent = self._owner
- if hasattr(item, 'name'):
+ if hasattr(item, 'name') and item.name != 'server':
self[item.name] = item
else:
self[hash(item)] = item
sidler@76 ~/repos/scottidler/nginx-config-builder/src master ● sudo -E ipython3
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.2 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from nginx.config.api import Config, Section, Location, KeyOption, KeyValueOption, KeyMultiValueOption
In [2]: http = Section('http', include='../conf/mime.types')
In [3]: server1 = Section('server', KeyValueOption('listen', '80'), KeyValueOption('server_name', 'server1.com'), KeyMultiValueOption('return', [301, 'https://$server_name$request_uri']))
In [4]: server2 = Section('server', KeyValueOption('listen', '443'), KeyValueOption('server_name', 'server2.com'), KeyMultiValueOption('return', [302, 'https://$server_name$request_uri']))
In [5]: http.sections.add(server1, server2)
In [6]: http
Out[6]:
http {
include ../conf/mime.types;
server {
listen 80;
server_name server1.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
server_name server2.com;
return 302 https://$server_name$request_uri;
}
}
It seems what is happening here is that it tests to see if the object as a 'name' field and if so, it uses this for the hash of the dictionary. In the case of the server blocks, the 'name' field is 'server' and therefore it only allows for one server block. Is this intentional? Is this a bug? Or is there another way that this is supposed to be used?
my usecase: change existing nginx config (add upstream, change location, add server block)
how i can achieve it?
common use case for reverse proxy is the following config:
server {
add_header Strict-Transport-Security max-age=31536000;
location /api/v1 {
proxy_pass http://127.0.0.1/;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
I mean proxy_set_header
options.
How i can achieve it with this library?
Hello, I'm trying to define some add_header
directives but I can't because the system detects it like repeated keyword, is possible to solve this?
Best!
Can we fix this already identified issue and release a new stable version. This is a good feature and is helpful.
thx for this nginx tool,
can nginx-config-builder support config of upstreams?
thx very much
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.