65
65
fi
66
66
fi
67
67
68
- # Generate additional route configurations based on environment variables
69
- ADDITIONAL_ROUTES=" "
68
+ # --- Dynamic Nginx config generation ---
69
+ declare -A SERVER_BLOCKS
70
+
70
71
i=1
71
72
while true ; do
73
+ HOST_VAR=" ROUTE_${i} _HOST"
72
74
PATH_VAR=" ROUTE_${i} _PATH"
73
75
DEST_VAR=" ROUTE_${i} _DESTINATION"
74
76
PORT_VAR=" ROUTE_${i} _PORT"
75
-
76
- if [ -z " ${! PATH_VAR} " ]; then
77
- break
78
- fi
79
77
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:-/ } "
81
83
ROUTE_DEST=" ${! DEST_VAR} "
82
84
ROUTE_PORT=" ${! PORT_VAR:- 80} "
83
85
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
+ }
96
98
"
99
+ SERVER_BLOCKS[" $ROUTE_HOST " ]+=" $LOCATION_BLOCK "
97
100
i=$(( i+ 1 ))
98
101
done
99
102
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
100
113
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
105
115
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
107
127
108
- # Start Nginx
109
128
log " Starting Nginx reverse proxy..."
110
- nginx -g " daemon off;"
129
+ nginx -g " daemon off;"
0 commit comments