Fix Nginx config generation

This commit is contained in:
Samuel Huang
2024-09-14 15:11:38 +10:00
parent 6d9a6dad42
commit f5f94776eb
6 changed files with 20 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
location LOCATION { location WEBPATH {
if ($content_type !~ "application/grpc") { if ($content_type !~ "application/grpc") {
return 404; return 404;
} }

View File

@@ -1,4 +1,4 @@
location LOCATION { location WEBPATH {
proxy_pass http://HOST:PORT; proxy_pass http://HOST:PORT;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;

View File

@@ -1,15 +1,15 @@
#STSTUB #STSTUB
stream { stream {
map $ssl_preread_server_name $stream_map { map $ssl_preread_server_name $stream_map {
#MAPSTUB #XMAP-TAG
} }
#UPSSTUB #XUPSTREAM-TAG
server { server {
listen STPORT reuseport; listen STPORT reuseport;
proxy_pass $stream_map; proxy_pass $stream_map;
# proxy_bind $remote_addr transparent;
ssl_preread on; ssl_preread on;
} }
} }

View File

@@ -1,4 +1,4 @@
location LOCATION { location WEBPATH {
if ($http_upgrade != "websocket") { if ($http_upgrade != "websocket") {
return 404; return 404;
} }

View File

@@ -54,9 +54,9 @@ if [ -z "${NGSVR}" ] && [ -z "${STPORT}" ]; then
exit 1; exit 1;
fi fi
# Running as root to enable low port listening. Necessary for Fargate or k8s. # Running as root to enable transparent stream.
# sed -i 's/^user nginx;$/user root;/g' /etc/nginx/nginx.conf # sed -i 's/^user \+nginx;$/user root;/g' /etc/nginx/nginx.conf
#mkdir -p /run/nginx/ # mkdir -p /run/nginx/
cd /etc/nginx/conf.d/ cd /etc/nginx/conf.d/
if [ -f /etc/nginx/conf.d/default.conf ]; then if [ -f /etc/nginx/conf.d/default.conf ]; then
@@ -94,8 +94,10 @@ if [ -n "${STPORT}" ]; then
echo " server $upstream;" >>/tmp/ups.conf echo " server $upstream;" >>/tmp/ups.conf
echo " }" >>/tmp/ups.conf echo " }" >>/tmp/ups.conf
done done
sed -i '/#MAPSTUB/r /tmp/map.conf' /etc/nginx/nginx.conf # Add map.conf down to #MAPSTUB tag
sed -i '/#UPSSTUB/r /tmp/ups.conf' /etc/nginx/nginx.conf 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 sed -i "s/STPORT/${STPORT}/g" /etc/nginx/nginx.conf
fi fi
@@ -179,24 +181,22 @@ do
for domain in "${xdomain[@]}" for domain in "${xdomain[@]}"
do do
if ! [ -f "${domain}.conf" ]; then echo "Assigned domain ${domain} not found"; usage; exit 1; fi 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 case "${xnetwork}" in
ws|websocket) ws|websocket)
sed -i -e "/^\}$/r nginx-ws.tpl" -e "/^\}$/d" ${domain}.conf sed -i '/#XLOCATION-TAG/r nginx-ws.tpl' ${domain}.conf
;; ;;
grpc) grpc)
sed -i -e "/^\}$/r nginx-grpc.tpl" -e "/^\}$/d" ${domain}.conf sed -i '/#XLOCATION-TAG/r nginx-grpc.tpl' ${domain}.conf
;; ;;
splt|proxy) splt|proxy)
sed -i -e "/^\}$/r nginx-proxy.tpl" -e "/^\}$/d" ${domain}.conf sed -i '/#XLOCATION-TAG/r nginx-proxy.tpl' ${domain}.conf
;; ;;
esac 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') ESC_LOCATION=$(printf '%s\n' "${xlocation}" | sed -e 's/[]\/$*.^[]/\\&/g')
sed -i "s/HOST/${xhost}/g" ${domain}.conf sed -i "s/HOST/${xhost}/g" ${domain}.conf
sed -i "s/PORT/${xport}/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
done done
exit 0 exit 0

View File

@@ -19,4 +19,5 @@ server {
return 404; return 404;
} }
} #XLOCATION-TAG
}