Skip to content

Commit bd728e5

Browse files
authored
server blocks
1 parent 84dd7b9 commit bd728e5

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

pritunl-nginx-proxy/entrypoint.sh

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,46 +65,65 @@ else
6565
fi
6666
fi
6767

68-
# Generate additional route configurations based on environment variables
69-
ADDITIONAL_ROUTES=""
68+
# --- Dynamic Nginx config generation ---
69+
declare -A SERVER_BLOCKS
70+
7071
i=1
7172
while true; do
73+
HOST_VAR="ROUTE_${i}_HOST"
7274
PATH_VAR="ROUTE_${i}_PATH"
7375
DEST_VAR="ROUTE_${i}_DESTINATION"
7476
PORT_VAR="ROUTE_${i}_PORT"
75-
76-
if [ -z "${!PATH_VAR}" ]; then
77-
break
78-
fi
7977

80-
ROUTE_PATH="${!PATH_VAR}"
78+
# Stop if no more routes
79+
[ -z "${!HOST_VAR}" ] && break
80+
81+
ROUTE_HOST="${!HOST_VAR}"
82+
ROUTE_PATH="${!PATH_VAR:-/}"
8183
ROUTE_DEST="${!DEST_VAR}"
8284
ROUTE_PORT="${!PORT_VAR:-80}"
8385

84-
log "Adding route: $ROUTE_PATH -> $ROUTE_DEST:$ROUTE_PORT"
85-
86-
ADDITIONAL_ROUTES="${ADDITIONAL_ROUTES}location ${ROUTE_PATH} {
87-
proxy_pass http://${ROUTE_DEST}:${ROUTE_PORT};
88-
proxy_http_version 1.1;
89-
proxy_set_header Upgrade \$http_upgrade;
90-
proxy_set_header Connection \"upgrade\";
91-
proxy_set_header Host \$host;
92-
proxy_set_header X-Real-IP \$remote_addr;
93-
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
94-
proxy_set_header X-Forwarded-Proto \$scheme;
95-
}
86+
log "Adding route: Host=$ROUTE_HOST Path=$ROUTE_PATH -> $ROUTE_DEST:$ROUTE_PORT"
87+
88+
LOCATION_BLOCK=" location ${ROUTE_PATH} {
89+
proxy_pass http://${ROUTE_DEST}:${ROUTE_PORT};
90+
proxy_http_version 1.1;
91+
proxy_set_header Upgrade \$http_upgrade;
92+
proxy_set_header Connection \"upgrade\";
93+
proxy_set_header Host \$host;
94+
proxy_set_header X-Real-IP \$remote_addr;
95+
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
96+
proxy_set_header X-Forwarded-Proto \$scheme;
97+
}
9698
"
99+
SERVER_BLOCKS["$ROUTE_HOST"]+="$LOCATION_BLOCK"
97100
i=$((i+1))
98101
done
99102

103+
# Build dynamic server blocks
104+
DYNAMIC_SERVER_BLOCKS=""
105+
for HOST in "${!SERVER_BLOCKS[@]}"; do
106+
DYNAMIC_SERVER_BLOCKS+="
107+
server {
108+
listen ${NGINX_PORT:-80};
109+
server_name $HOST;
110+
${SERVER_BLOCKS[$HOST]}
111+
}"
112+
done
100113

101-
# Export the additional routes for envsubst
102-
export ADDITIONAL_ROUTES
103-
104-
# Process the Nginx configuration template
114+
# Process template and inject dynamic blocks
105115
log "Generating Nginx configuration..."
106-
envsubst '${PROXY_PASS_DEFAULT} ${PROXY_PORT_DEFAULT} ${NGINX_PORT} ${ADDITIONAL_ROUTES}' < /etc/nginx/templates/nginx.conf.template > /etc/nginx/nginx.conf
116+
export DYNAMIC_SERVER_BLOCKS
117+
118+
envsubst '${NGINX_PORT} ${PROXY_PASS_DEFAULT} ${PROXY_PORT_DEFAULT} ${ADDITIONAL_ROUTES}' \
119+
< /etc/nginx/templates/nginx.conf.template > /etc/nginx/nginx.conf.tmp
120+
121+
# Inject dynamic server blocks before closing HTTP block
122+
sed -i '/^}$/i '"${DYNAMIC_SERVER_BLOCKS}" /etc/nginx/nginx.conf.tmp
123+
mv /etc/nginx/nginx.conf.tmp /etc/nginx/nginx.conf
124+
125+
log "Final Nginx configuration:"
126+
cat /etc/nginx/nginx.conf
107127

108-
# Start Nginx
109128
log "Starting Nginx reverse proxy..."
110-
nginx -g "daemon off;"
129+
nginx -g "daemon off;"

0 commit comments

Comments
 (0)