-
Notifications
You must be signed in to change notification settings - Fork 11
/
generate_nginx_standalone.py
179 lines (147 loc) · 5.23 KB
/
generate_nginx_standalone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
import sys
import os
import argparse
import validators
from urllib.parse import urlparse
def generate_nginx_defaults(args):
result_url = urlparse(args.server_base_url)
# if .path property is empty it means the url is just the domain w/ no additional route (e.g.: != http://sub.domain.tld/custom-route/)
if result_url.path == '/' or result_url.path == '':
custom_route = ''
http_proxy_location = ''
https_proxy_location = ''
static_block = """
location /static {
alias /opt/mhn/static;
}
"""
# if .path property not empty, it means some path defined after tld (e.g.: sub.domain.tld/custom/stuff/here/)
else:
custom_path = result_url.path
# if there are one or more leading or trailing slashes, remove them
custom_path = custom_path.strip('/')
# now, custom_route should contain a str like 'custom/stuff/here'
custom_route = custom_path
http_proxy_location = """
location / {{
proxy_pass http://localhost/{custom_route}/;
proxy_redirect off;
proxy_read_timeout 60s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}}
""".format(custom_route=custom_route)
https_proxy_location = """
location / {{
proxy_pass https://localhost/{custom_route}/;
proxy_redirect off;
proxy_read_timeout 60s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}}
""".format(custom_route=custom_route)
static_block = """
location /{custom_route}/static {{
alias /opt/mhn/static;
}}
location = /static {{
rewrite ^ /{custom_route}/static;
}}
""".format(custom_route=custom_route)
chnserver_template = """# Generated from generate_nginx_default_config.py
# this file is read from /opt/nginx/sites-available/default
server {{
listen 80;
server_name _;
location /{custom_route} {{
try_files \\$uri @mhnserver;
}}
root /opt/www;
location @mhnserver {{
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}}
{http_proxy_location}
{static_block}
}}
server {{
#server_name
listen 443 ssl http2;
listen [::]:443 ssl http2;
# certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
ssl_certificate /tls/cert.pem;
ssl_certificate_key /tls/key.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
# modern configuration. tweak to your needs.
ssl_protocols TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
ssl_prefer_server_ciphers on;
# HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
# add_header Strict-Transport-Security max-age=15768000;
# OCSP Stapling ---
# fetch OCSP records from URL in ssl_certificate and cache them
ssl_stapling on;
ssl_stapling_verify on;
## verify chain of trust of OCSP response using Root CA and Intermediate certs
## only use this with real (non-self-signed) certs
#ssl_trusted_certificate /etc/pki/tls/certs/cert.pem;
# resolver <IP DNS resolver>;
location /{custom_route} {{
try_files \\$uri @mhnserver;
}}
root /opt/www;
location @mhnserver {{
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}}
{https_proxy_location}
{static_block}
}}
""".format(
custom_route=custom_route,
http_proxy_location=http_proxy_location,
https_proxy_location=https_proxy_location,
static_block=static_block
)
if not os.path.exists(args.output_file_nginx) or args.force_overwrite:
f = open(args.output_file_nginx, 'w')
f.write(chnserver_template)
f.close()
print("Wrote file to %s" % args.output_file_nginx)
else:
sys.stderr.write("Not writing file, add -f to override\n")
def check_url(url):
"""
Make sure this is a real URL
"""
if validators.url(url):
return url
else:
raise argparse.ArgumentTypeError("%s is an invalid url" % url)
def parse_args():
parser = argparse.ArgumentParser(
description='Generate nginx default conf file using some sane defaults'
)
parser.add_argument(
'-s', '--server-base-url', help='Public URL for your CHN Server',
required=True, type=check_url
)
parser.add_argument(
'-n', '--output-file-nginx', required=True,
help='File path to write nginx default out to'
)
parser.add_argument(
'-f', '--force-overwrite', action='store_true',
help='Forcibly overwrite file, even if it exists'
)
return parser.parse_args()
def main():
args = parse_args()
generate_nginx_defaults(args)
if __name__ == "__main__":
sys.exit(main())