-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-config.py
More file actions
78 lines (59 loc) · 2.39 KB
/
create-config.py
File metadata and controls
78 lines (59 loc) · 2.39 KB
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
import os
SSL_PORTS = os.environ.get('SSL_PORTS')
PORTS = os.environ.get('PORTS')
SERVERNAMES = os.environ.get('SERVERNAMES')
CONTAINER_ADDR = os.environ.get('CONTAINER_ADDR')
if not CONTAINER_ADDR:
raise ValueError('No container address provided')
SERVERNAMES = SERVERNAMES.replace(',', ' ') + ' localhost' if SERVERNAMES else 'localhost'
SSL_PORTS = SSL_PORTS.split(',') if SSL_PORTS else ["443"]
PORTS = PORTS.split(',') if PORTS else ["80"]
CERTPATH = '/certs/server.crt'
KEYPATH = '/certs/server.key'
if os.path.isdir('/etc/letsencrypt/live'):
CERTPATH = '/etc/letsencrypt/live/{}/fullchain.pem'.format(SERVERNAMES)
KEYPATH = '/etc/letsencrypt/live/{}/privkey.pem'.format(SERVERNAMES)
full_config = ''
for port in SSL_PORTS:
CONFIG_STR = '''
server {{
listen {port} ssl http2;
listen [::]:{port} ssl http2;
server_name {servername};
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
# add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
ssl_certificate {certpath};
ssl_certificate_key {keypath};
location / {{
proxy_pass http://{container_addr};
}}
}}
'''.format(port=port, servername=SERVERNAMES, container_addr=CONTAINER_ADDR, certpath=CERTPATH, keypath=KEYPATH)
full_config += CONFIG_STR
for port in PORTS:
CONFIG_STR = '''
server {{
listen {port};
listen [::]:{port};
server_name {servername};
location ~ /.well-known/acme-challenge {{
allow all;
root /var/www/html;
}}
location / {{
rewrite ^ https://$host$request_uri? permanent;
}}
}}
'''.format(port=port, servername=SERVERNAMES)
full_config += CONFIG_STR
# check if /etc/nginx/default.conf exists
if not os.path.exists('/etc/nginx/proxy.conf'):
with open('/etc/nginx/conf.d/proxy.conf', 'w') as f:
f.write(full_config)
# move /etc/nginx/default.conf to /etc/nginx/default.conf.bak
os.rename('/etc/nginx/conf.d/default.conf', '/etc/nginx/conf.d/default.conf.bak')
print(full_config)