-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerateNginxConfigsForLoadbalancing.sh
executable file
·424 lines (405 loc) · 18.7 KB
/
generateNginxConfigsForLoadbalancing.sh
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/bin/bash
# this script generates a bunch of sites in sites_enabled and links them from sites_available
# corresponding to the configs in the directory "loadbalancing"
#
# afterwards it tests the configuration if possible ( works on debian/ubuntu as root ), but will not restart nginx.
# you have to do that yourself.
#
# the following prefix will be used for the filenames, all files matching this prefix will be deleted first.
prefix="loadbalancing_"
# start of script
dir=`dirname $0`
cd "$dir"
if ! [ -f "`basename $0`" ]; then
echo "ERROR: can not find myself in $dir".
exit 1
fi
if ! [ -d "loadbalancing" ]; then
echo "ERROR: can not find directory "loadbalancing" in $dir".
exit 1
fi
rm sites-enabled/${prefix}*
rm sites-available/${prefix}*
mkdir -p logins
rm logins/*
names=""
defaultSet=""
for i in loadbalancing/*; do
# skip non-files
[ -f "$i" ] || continue;
# reset config
name=""; domains=""
default=false
backendsHttp=""; backendsHttps=""
backendsHttpFallback=""; backendsHttpsFallback=""
forwardHttp2Https=false; forwardHttps2Http=false
forwardHttp=""; forwardHttps="";
httpsInternalHttp=false
enableFallback=false
fallbackErrorCodes=""
login=""; password=""; authFile=""
disabled=""; staticSite=""
# read config
. "$i"
if [ -n "$disabled" ]; then
continue;
fi
# check sanity
if [ "$name" = "" ]; then
echo "ERROR: no name specfied for $i. skipping ... "
continue;
fi
if echo "$names" | grep "^$name "; then
echo "ERROR: duplicate name $name in $i. skipping ..."
continue;
fi
names=$(echo "$names";echo "$name already defined in $i")
if $default; then
if [ -n "$defaultSet" ]; then
echo "ERROR: default server already set as "$defaultSet". skipping $i ..."
continue;
else
defaultSet="$name"
fi
fi
if [ "$domains" = "" ]; then
echo "ERROR: no domains specfied for $name. skipping $i"
continue;
fi
if ( ( [ "$backendsHttp" = "" ] && [ "$forwardHttp" = "" ] ) || $forwardHttp2Https ) && ( ( [ "$backendsHttps" = "" ] && [ "$forwardHttps" = "" ] ) || $forwardHttps2Http ); then
echo "ERROR: neither http or https backend for $name. what do you actually want? skipping $i ..."
continue;
fi
if $forwardHttp2Https && $forwardHttps2Http; then
echo "ERROR: both forward http⇒https and https⇒http enabled. are you crazy?? skipping $i ..."
continue;
fi
if $forwardHttp2Https && [ "$forwardHttp" != "" ]; then
echo "ERROR: you have forwardHttp2Https and forwardHttp enabled. Not sure what to do... skipping $i ..."
continue;
fi
if $forwardHttps2Http && [ "$forwardHttps" != "" ]; then
echo "ERROR: you have forwardHttps2Http and forwardHttps enabled. Not sure what to do... skipping $i ..."
continue;
fi
if $default && [ "$forwardHttps" != "" ]; then
echo "ERROR: sorry, but wildcard forwarding doesn't work with ssl... skipping $i ..."
continue;
fi
if $enableFallback && [ "$fallbackErrorCodes" = "" ]; then
echo "ERROR: Fallback is enabled but no error codes for fallback are defined. skipping $i ..."
continue;
fi
if $enableFallback && ( ( [ "$backendsHttp" != "" ] && [ "$backendsHttpFallback" = "" ] ) || ( [ "$backendsHttps" != "" ] && [ "$backendsHttpsFallback" = "" ] ) ); then
echo "ERROR: Fallback is enabled but not all required backends are configured. skipping $i ..."
continue;
fi
if [ -n "$login" ]; then
# generate authontication-file (htpasswd) for HTTP basic authentication
authFile=`pwd`/logins/htpasswd_$name
htpasswd -nb $login $password >> $authFile
fi
# generate nginx config file
fname="$prefix$name"
echo "generating config file for $name ..."
{
# http part
if [ -n "$backendsHttp" ]; then
echo "
upstream upstream_$name {
# send requests from single ips to a single backend server
ip_hash;
# backends"
echo -n "$backendsHttp" | sed 's/\(\S*\)/\1\n/g' | sed 's/\*/ /' | { while read backend weight; do
add=""
if [ -n "$weight" ]; then
if echo "$weight" | grep -q '[0-9]*'; then
add="$add weight=$weight"
else
echo "WARNING: weight for backend $backend is $weight (non-numeric). No weight applied!"
fi
fi
echo " server $backend$add;"
done; }
if [ -n "$backendsHttpFallback" ]; then
echo " }
upstream upstream_${name}_fallback {
# send requests from single ips to a single backend server
ip_hash;
# backends"
echo -n "$backendsHttpFallback" | sed 's/\(\S*\)/\1\n/g' | sed 's/\*/ /' | { while read backend weight; do
add=""
if [ -n "$weight" ]; then
if echo "$weight" | grep -q '[0-9]*'; then
add="$add weight=$weight"
else
echo "WARNING: weight for backend $backend is $weight (non-numeric). No weight applied!"
fi
fi
echo " server $backend$add;"
done; }
fi
echo "
}
server {
listen 80"$( $default && echo ' default_server default' )";
server_name $domains;
access_log /var/log/nginx/access_$fname.log;
error_log /var/log/nginx/error_$fname.log;"
if $enableFallback; then
echo " recursive_error_pages on;
proxy_intercept_errors on;"
fi
if [ -n "$staticSite" ]; then
echo "
location / {
root /etc/nginx/loadbalancing/static;
try_files \$uri /$staticSite;"
if [ -n "$authFile" ]; then
echo " auth_basic \"Please authenticate for $name\";"
echo " auth_basic_user_file $authFile;"
fi
echo "
}
location = /$staticSite {"
if [ -n "$authFile" ]; then
echo " auth_basic \"Please authenticate for $name\";"
echo " auth_basic_user_file $authFile;"
fi
echo "
return 503;
}
error_page 503 @maintenance;
location @maintenance {
root /etc/nginx/loadbalancing/static;
rewrite ^.*$ /$staticSite break;
}"
else
echo "
location / {
proxy_pass http://upstream_$name;
proxy_next_upstream error invalid_header timeout http_502 http_504;
proxy_redirect off;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
client_max_body_size 32m;
client_body_buffer_size 1m;
proxy_connect_timeout 3; # timeout for connection to backend
proxy_send_timeout 5; # timeout between write-requests of a single connection
proxy_read_timeout 120;"
if [ -n "$authFile" ]; then
echo " auth_basic \"Please authenticate for $name\";"
echo " auth_basic_user_file $authFile;"
fi
if $enableFallback; then
echo " error_page $fallbackErrorCodes = @fallback;"
fi
echo "
}"
if $enableFallback; then
echo "
location @fallback {
proxy_pass http://upstream_${name}_fallback;
proxy_next_upstream error invalid_header timeout http_502 http_504;
proxy_redirect off;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
client_max_body_size 32m;
client_body_buffer_size 1m;
proxy_connect_timeout 3; # timeout for connection to backend
proxy_send_timeout 5; # timeout between write-requests of a single connection
proxy_read_timeout 120;"
if [ -n "$authFile" ]; then
echo " auth_basic \"Please authenticate for $name\";"
echo " auth_basic_user_file $authFile;"
fi
echo "
}"
fi
fi
echo "
}
"
elif $forwardHttp2Https; then
echo "
server {
listen 80"$( $default && echo ' default_server default' )";
access_log /var/log/nginx/access_$fname.log;
error_log /var/log/nginx/error_$fname.log;
server_name $domains;
rewrite ^ https://\$server_name\$request_uri? permanent;
}"
elif [ -n "$forwardHttp" ]; then
echo "
server {
listen 80"$( $default && echo ' default_server default' )";
access_log /var/log/nginx/access_$fname.log;
error_log /var/log/nginx/error_$fname.log;
server_name $domains;
rewrite ^ ${forwardHttp}\$request_uri? permanent;
}"
fi
# https part
if [ -n "$backendsHttps" ]; then
echo "
upstream upstreamSecure_$name {
# send requests from single ips to a single backend server
ip_hash;
# backends"
echo -n "$backendsHttps" | sed 's/\(\S*\)/\1\n/g' | sed 's/\*/ /' | { while read backend weight; do
add=""
if ! ( $httpsInternalHttp || echo "$backend" | grep -qF ':' ) ; then
add=":443"
fi
if [ -n "$weight" ]; then
if echo "$weight" | grep -q '[0-9]*'; then
add="$add weight=$weight"
else
echo "WARNING: weight for backend $backend is $weight (non-numeric). No weight applied!"
fi
fi
echo " server $backend$add;"
done; }
if [ -n "$backendsHttpsFallback" ]; then
echo " }
upstream upstreamSecure_${name}_fallback {
# send requests from single ips to a single backend server
ip_hash;
# backends"
echo -n "$backendsHttpsFallback" | sed 's/\(\S*\)/\1\n/g' | sed 's/\*/ /' | { while read backend weight; do
add=""
if ! ( $httpsInternalHttp || echo "$backend" | grep -qF ':' ) ; then
add=":443"
fi
if [ -n "$weight" ]; then
if echo "$weight" | grep -q '[0-9]*'; then
add="$add weight=$weight"
else
echo "WARNING: weight for backend $backend is $weight (non-numeric). No weight applied!"
fi
fi
echo " server $backend$add;"
done; }
fi
echo "
}
server {
listen 443"$( $default && echo ' default_server' )";
access_log /var/log/nginx/access_$fname.log;
error_log /var/log/nginx/error_$fname.log;
server_name $domains;
ssl on;
ssl_certificate "/etc/ssl/private/_mibaby_de_bundle.pem";
ssl_certificate_key "/etc/ssl/private/_mibaby_de.pem";
#ssl_client_certificate "/etc/ssl/private/CA-bundle.pem";
add_header X-Forwarded-Ssl on;
add_header X-Forwarded-Proto https;
add_header X-Forwarded-Port 443;"
if $enableFallback; then
echo " recursive_error_pages on;
proxy_intercept_errors on;"
fi
if [ -n "$staticSite" ]; then
echo "
location / {
root /etc/nginx/loadbalancing/static;
try_files \$uri /$staticSite;"
if [ -n "$authFile" ]; then
echo " auth_basic \"Please authenticate for $name\";"
echo " auth_basic_user_file $authFile;"
fi
echo "
}
location = /$staticSite {"
if [ -n "$authFile" ]; then
echo " auth_basic \"Please authenticate for $name\";"
echo " auth_basic_user_file $authFile;"
fi
echo "
return 503;
}
error_page 503 @maintenance;
location @maintenance {
root /etc/nginx/loadbalancing/static;
rewrite ^.*$ /$staticSite break;
}"
else
echo "
location / {
proxy_pass http$( $httpsInternalHttp || echo -n s )://upstreamSecure_$name;
proxy_next_upstream error invalid_header timeout http_502 http_504;
proxy_redirect off;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
client_max_body_size 32m;
client_body_buffer_size 1m;
proxy_connect_timeout 3; # timeout for connection to backend
proxy_send_timeout 5; # timeout between write-requests of a single connection
proxy_read_timeout 120; # timeout between read-requests of a single connection"
if [ -n "$authFile" ]; then
echo " auth_basic \"Please authenticate for $name\";"
echo " auth_basic_user_file $authFile;"
fi
if $enableFallback; then
echo " error_page $fallbackErrorCodes = @fallback;"
fi
echo "
}"
if $enableFallback; then
echo "
location @fallback {
proxy_pass http$( $httpsInternalHttp || echo -n s )://upstreamSecure_${name}_fallback;
proxy_next_upstream error invalid_header timeout http_502 http_504;
proxy_redirect off;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
client_max_body_size 32m;
client_body_buffer_size 1m;
proxy_connect_timeout 3; # timeout for connection to backend
proxy_send_timeout 5; # timeout between write-requests of a single connection
proxy_read_timeout 120;"
if [ -n "$authFile" ]; then
echo " auth_basic \"Please authenticate for $name\";"
echo " auth_basic_user_file $authFile;"
fi
echo "
}"
fi
fi
echo "
}
"
elif $forwardHttps2Http; then
echo "
server {
listen 443"$( $default && echo ' default_server default' )";
access_log /var/log/nginx/access_$fname.log;
error_log /var/log/nginx/error_$fname.log;
ssl_certificate "/etc/ssl/private/_mibaby_de_bundle.pem";
ssl_certificate_key "/etc/ssl/private/_mibaby_de.pem";
#ssl_client_certificate "/etc/ssl/private/CA-bundle.pem";
server_name $domains;
rewrite ^ http://\$server_name\$request_uri? permanent;
}"
elif [ -n "$forwardHttps" ]; then
echo "
server {
listen 443;
access_log /var/log/nginx/access_$fname.log;
error_log /var/log/nginx/error_$fname.log;
ssl_certificate "/etc/ssl/private/_mibaby_de_bundle.pem";
ssl_certificate_key "/etc/ssl/private/_mibaby_de.pem";
#ssl_client_certificate "/etc/ssl/private/CA-bundle.pem";
server_name $domains;
rewrite ^ ${forwardHttps}\$request_uri? permanent;
}"
fi
} > sites-available/$fname
ln -s ../sites-available/$fname sites-enabled/$fname
done
echo "Testing configuration. if it succeeds, use \"service nginx reload\" or \"service nginx restart\""
service nginx configtest