Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
itsalfredakku committed Aug 26, 2024
1 parent d184de8 commit 582c194
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions proxy.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash

# Read the PROXY environment variable
PROXY=${PROXY:?Need to set PROXY environment variable. Format: PROXY=- listen: port1 upstream: host1:port1, ...}
# Read the LISTEN and UPSTREAM environment variables
LISTEN_9000_UPSTREAMS=${LISTEN_9000_UPSTREAMS:?Need to set LISTEN_9000_UPSTREAMS environment variable. Format: upstream_host1:upstream_port1,upstream_host2:upstream_port2,...}
LISTEN_9001_UPSTREAMS=${LISTEN_9001_UPSTREAMS:?Need to set LISTEN_9001_UPSTREAMS environment variable. Format: upstream_host1:upstream_port1,upstream_host2:upstream_port2,...}

# Start building the nginx.conf file
cat > /etc/nginx/nginx.conf <<EOL
Expand All @@ -12,19 +13,43 @@ events {
http {
EOL

# Parse the PROXY variable and create server and upstream blocks
echo "$PROXY" | awk '
/^- listen:/ { listen_port = $2 }
/^ upstream:/ { upstream_host_port = substr($0, index($0, ":") + 1) }
END {
split(upstream_host_port, arr, ":")
upstream_host = arr[1]
upstream_port = arr[2]
# Parse and create upstream and server blocks for port 9000
IFS=',' read -ra UPSTREAMS_9000 <<< "$LISTEN_9000_UPSTREAMS"
for upstream in "${UPSTREAMS_9000[@]}"; do
IFS=':' read upstream_host upstream_port <<< "$upstream"
echo " upstream upstream_${upstream_port} { server ${upstream_host}:${upstream_port}; }" >> /etc/nginx/nginx.conf
cat >> /etc/nginx/nginx.conf <<EOL
server {
listen 9000;
location / {
proxy_pass http://upstream_${upstream_port};
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;
}
}
EOL
done

print " upstream upstream_" upstream_port " { server " upstream_host ":" upstream_port "; }" >> "/etc/nginx/nginx.conf"
print " server { listen " listen_port "; location / { proxy_pass http://upstream_" upstream_port "; 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; } }" >> "/etc/nginx/nginx.conf"
}
'
# Parse and create upstream and server blocks for port 9001
IFS=',' read -ra UPSTREAMS_9001 <<< "$LISTEN_9001_UPSTREAMS"
for upstream in "${UPSTREAMS_9001[@]}"; do
IFS=':' read upstream_host upstream_port <<< "$upstream"
echo " upstream upstream_${upstream_port} { server ${upstream_host}:${upstream_port}; }" >> /etc/nginx/nginx.conf
cat >> /etc/nginx/nginx.conf <<EOL
server {
listen 9001;
location / {
proxy_pass http://upstream_${upstream_port};
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;
}
}
EOL
done

# Close the http block
echo "}" >> /etc/nginx/nginx.conf
Expand Down

0 comments on commit 582c194

Please sign in to comment.