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") {
return 404;
}

View File

@@ -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;

View File

@@ -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;
}
}

View File

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

View File

@@ -54,8 +54,8 @@ 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
# 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/
@@ -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

View File

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