-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfigure_nginx.sh
53 lines (43 loc) · 1.47 KB
/
configure_nginx.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
#!/bin/bash
# Variable to store the static content of nginx.conf
nginx_conf="worker_processes 1;
events {
worker_connections 1024;
}
http {
# Increase the bucket size for server names hash tables
server_names_hash_bucket_size 128;
"
# Split SERVER_NAME and PROXY_PASS into arrays
IFS=',' read -ra server_name_array <<< "${SERVER_NAME}"
IFS=',' read -ra proxy_pass_array <<< "${PROXY_PASS}"
# Check if the lengths of the arrays match
if [ ${#server_name_array[@]} -ne ${#proxy_pass_array[@]} ]; then
echo "Error: The number of server names and proxy pass values do not match."
exit 1
fi
# Iterate over the server name and proxy pass value arrays to generate server blocks
for ((i = 0; i < ${#server_name_array[@]}; i++)); do
# Append the server block to the nginx configuration string
nginx_conf+="
server {
listen ${PORT};
server_name ${server_name_array[$i]};
location / {
auth_basic \"Restricted\";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_http_version 1.1;
proxy_pass ${proxy_pass_array[$i]};
}
}"
done
# End the http {} block and complete the nginx configuration string
nginx_conf+="
}"
echo "Generated nginx.conf:"
echo "$nginx_conf"
echo "$nginx_conf" > /etc/nginx/nginx.conf