-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.sh
117 lines (100 loc) · 3.67 KB
/
proxy.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
#!/bin/bash
# Start building the nginx.conf file
cat > /etc/nginx/nginx.conf <<EOL
worker_processes 1;
events {
worker_connections 1024;
}
http {
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
EOL
# Print environment variables for debugging
echo "Available environment variables:"
printenv
# Loop over all environment variables starting with 'LISTEN_'
for var in $(printenv | grep -Eo '^LISTEN_[0-9]+(_WSS)?'); do
# Extract port number and suffix
if [[ "$var" =~ LISTEN_([0-9]+)(_[A-Z]+)? ]]; then
port="${BASH_REMATCH[1]}"
suffix="${BASH_REMATCH[2]}"
else
echo "Unable to extract port from ${var}, skipping..."
continue
fi
upstreams=$(printenv "$var")
# Debugging output
echo "Processing port: ${port}"
echo "Upstreams: ${upstreams}"
# Determine if WebSocket configuration should be added
if [[ "$suffix" == "_WSS" ]]; then
echo "WebSocket configuration detected for port ${port}"
ws_config="
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection \"upgrade\";
"
# Parse and create upstream and server blocks for each port
IFS=',' read -ra UPSTREAMS <<< "$upstreams"
upstream_block_name="upstream_${port}"
# Define upstream block
echo " upstream ${upstream_block_name} {" >> /etc/nginx/nginx.conf
for upstream in "${UPSTREAMS[@]}"; do
IFS=':' read -r upstream_host upstream_port <<< "$upstream"
if [[ -z "$upstream_host" || -z "$upstream_port" ]]; then
echo "Invalid upstream format: ${upstream}, skipping..."
continue
fi
echo " server ${upstream_host}:${upstream_port};" >> /etc/nginx/nginx.conf
done
echo " }" >> /etc/nginx/nginx.conf
else
if [ -z "$upstreams" ]; then
echo "No upstreams defined for port ${port}, skipping..."
continue
fi
# Parse and create upstream and server blocks for each port
IFS=',' read -ra UPSTREAMS <<< "$upstreams"
upstream_block_name="upstream_${port}"
# Define upstream block
echo " upstream ${upstream_block_name} {" >> /etc/nginx/nginx.conf
for upstream in "${UPSTREAMS[@]}"; do
IFS=':' read -r upstream_host upstream_port <<< "$upstream"
if [[ -z "$upstream_host" || -z "$upstream_port" ]]; then
echo "Invalid upstream format: ${upstream}, skipping..."
continue
fi
echo " server ${upstream_host}:${upstream_port};" >> /etc/nginx/nginx.conf
done
echo " }" >> /etc/nginx/nginx.conf
fi
# Add a server block for this port
cat >> /etc/nginx/nginx.conf <<EOL
server {
listen ${port};
# ignore_invalid_headers off;
client_max_body_size 0;
proxy_buffering off;
proxy_request_buffering off;
location / {
${upstream_block_name:+proxy_pass http://${upstream_block_name};}
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_connect_timeout 300;
# chunked_transfer_encoding off;
$ws_config
}
}
EOL
done
# Close the http block
echo "}" >> /etc/nginx/nginx.conf
# Print the generated configuration for debugging
echo "Generated nginx.conf:"
cat /etc/nginx/nginx.conf
# Test Nginx configuration
nginx -t
# Start Nginx
nginx -g "daemon off;"