From f5f94776eb3b95a9f5081f1177819674ca446700 Mon Sep 17 00:00:00 2001 From: Samuel Huang Date: Sat, 14 Sep 2024 15:11:38 +1000 Subject: [PATCH] Fix Nginx config generation --- nginx-grpc.tpl | 2 +- nginx-proxy.tpl | 2 +- nginx-stream.tpl | 6 +++--- nginx-ws.tpl | 2 +- server-nginx.sh | 24 ++++++++++++------------ site-ssl.conf.tpl | 3 ++- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/nginx-grpc.tpl b/nginx-grpc.tpl index 2e3a1e7..61379c0 100644 --- a/nginx-grpc.tpl +++ b/nginx-grpc.tpl @@ -1,4 +1,4 @@ - location LOCATION { + location WEBPATH { if ($content_type !~ "application/grpc") { return 404; } diff --git a/nginx-proxy.tpl b/nginx-proxy.tpl index d6ec8bf..866d50e 100644 --- a/nginx-proxy.tpl +++ b/nginx-proxy.tpl @@ -1,4 +1,4 @@ - location LOCATION { + location WEBPATH { proxy_pass http://HOST:PORT; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/nginx-stream.tpl b/nginx-stream.tpl index 70ce746..f233d05 100644 --- a/nginx-stream.tpl +++ b/nginx-stream.tpl @@ -1,15 +1,15 @@ - #STSTUB stream { map $ssl_preread_server_name $stream_map { - #MAPSTUB + #XMAP-TAG } - #UPSSTUB + #XUPSTREAM-TAG server { listen STPORT reuseport; proxy_pass $stream_map; + # proxy_bind $remote_addr transparent; ssl_preread on; } } diff --git a/nginx-ws.tpl b/nginx-ws.tpl index 062fc7f..653ef9c 100644 --- a/nginx-ws.tpl +++ b/nginx-ws.tpl @@ -1,4 +1,4 @@ - location LOCATION { + location WEBPATH { if ($http_upgrade != "websocket") { return 404; } diff --git a/server-nginx.sh b/server-nginx.sh index b3547af..1502a67 100755 --- a/server-nginx.sh +++ b/server-nginx.sh @@ -54,9 +54,9 @@ if [ -z "${NGSVR}" ] && [ -z "${STPORT}" ]; then exit 1; fi -# Running as root to enable low port listening. Necessary for Fargate or k8s. -# sed -i 's/^user nginx;$/user root;/g' /etc/nginx/nginx.conf -#mkdir -p /run/nginx/ +# Running as root to enable transparent stream. +# sed -i 's/^user \+nginx;$/user root;/g' /etc/nginx/nginx.conf +# mkdir -p /run/nginx/ cd /etc/nginx/conf.d/ if [ -f /etc/nginx/conf.d/default.conf ]; then @@ -94,8 +94,10 @@ if [ -n "${STPORT}" ]; then echo " server $upstream;" >>/tmp/ups.conf echo " }" >>/tmp/ups.conf done - sed -i '/#MAPSTUB/r /tmp/map.conf' /etc/nginx/nginx.conf - sed -i '/#UPSSTUB/r /tmp/ups.conf' /etc/nginx/nginx.conf + # Add map.conf down to #MAPSTUB tag + sed -i '/#XMAP-TAG/r /tmp/map.conf' /etc/nginx/nginx.conf + # Add ups.conf down to #UPSSTUB tag + sed -i '/#XUPSTREAM-TAG/r /tmp/ups.conf' /etc/nginx/nginx.conf sed -i "s/STPORT/${STPORT}/g" /etc/nginx/nginx.conf fi @@ -179,24 +181,22 @@ do for domain in "${xdomain[@]}" do if ! [ -f "${domain}.conf" ]; then echo "Assigned domain ${domain} not found"; usage; exit 1; fi - # Replace the last(only) single line '}' with the content of tpl file, hence insert a new section into the Nginx config file + # Add tpl file content down to #LOCATION tag case "${xnetwork}" in ws|websocket) - sed -i -e "/^\}$/r nginx-ws.tpl" -e "/^\}$/d" ${domain}.conf + sed -i '/#XLOCATION-TAG/r nginx-ws.tpl' ${domain}.conf ;; grpc) - sed -i -e "/^\}$/r nginx-grpc.tpl" -e "/^\}$/d" ${domain}.conf + sed -i '/#XLOCATION-TAG/r nginx-grpc.tpl' ${domain}.conf ;; splt|proxy) - sed -i -e "/^\}$/r nginx-proxy.tpl" -e "/^\}$/d" ${domain}.conf + sed -i '/#XLOCATION-TAG/r nginx-proxy.tpl' ${domain}.conf ;; esac - # Then add '}' to the end of the Nginx config file - echo -e "\n}" >> ${domain}.conf ESC_LOCATION=$(printf '%s\n' "${xlocation}" | sed -e 's/[]\/$*.^[]/\\&/g') sed -i "s/HOST/${xhost}/g" ${domain}.conf sed -i "s/PORT/${xport}/g" ${domain}.conf - sed -i "s/LOCATION/${ESC_LOCATION}/g" ${domain}.conf + sed -i "s/WEBPATH/${ESC_LOCATION}/g" ${domain}.conf done done exit 0 diff --git a/site-ssl.conf.tpl b/site-ssl.conf.tpl index da0a64e..e6cd6f2 100644 --- a/site-ssl.conf.tpl +++ b/site-ssl.conf.tpl @@ -19,4 +19,5 @@ server { return 404; } -} \ No newline at end of file + #XLOCATION-TAG +}