fix: nginx query parameter redirect works like node.js

This commit is contained in:
Mitchell Scott
2025-10-20 11:42:42 -06:00
parent 8b076db25d
commit bca9376edc
2 changed files with 38 additions and 2 deletions

View File

@@ -10,8 +10,10 @@ server {
add_header X-Weatherstar true always; add_header X-Weatherstar true always;
include /etc/nginx/includes/wsqs_redirect.conf;
location / { location / {
index redirect.html index.html index.htm; index index.html index.htm;
try_files $uri $uri/ =404; try_files $uri $uri/ =404;
} }

View File

@@ -12,6 +12,9 @@ url_encode() {
# build query string from WSQS_ env vars # build query string from WSQS_ env vars
while IFS='=' read -r key val; do while IFS='=' read -r key val; do
# Skip empty lines
[ -z "$key" ] && continue
# Remove WSQS_ prefix and convert underscores to hyphens # Remove WSQS_ prefix and convert underscores to hyphens
key="${key#WSQS_}" key="${key#WSQS_}"
key="${key//_/-}" key="${key//_/-}"
@@ -23,11 +26,16 @@ while IFS='=' read -r key val; do
QS="${key}=${encoded_val}" QS="${key}=${encoded_val}"
fi fi
done << EOF done << EOF
$(env | grep '^WSQS_') $(env | grep '^WSQS_' || true)
EOF EOF
mkdir -p /etc/nginx/includes
if [ -n "$QS" ]; then if [ -n "$QS" ]; then
# Escape the query string for use in JavaScript (escape backslashes and single quotes)
QS_ESCAPED=$(printf '%s' "$QS" | sed "s/\\\\/\\\\\\\\/g; s/'/\\\'/g")
# Generate redirect.html with JavaScript logic
cat > "$ROOT/redirect.html" <<EOF cat > "$ROOT/redirect.html" <<EOF
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@@ -35,10 +43,36 @@ if [ -n "$QS" ]; then
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>Redirecting</title> <title>Redirecting</title>
<meta http-equiv="refresh" content="0;url=/index.html?$QS" /> <meta http-equiv="refresh" content="0;url=/index.html?$QS" />
<script>
(function() {
var wsqsParams = '$QS_ESCAPED';
var currentParams = window.location.search.substring(1);
var targetParams = currentParams || wsqsParams;
window.location.replace('/index.html?' + targetParams);
})();
</script>
</head> </head>
<body></body> <body></body>
</html> </html>
EOF EOF
# Generate nginx config for conditional redirects
cat > /etc/nginx/includes/wsqs_redirect.conf <<'EOF'
location = / {
if ($args = '') {
rewrite ^ /redirect.html last;
}
rewrite ^/$ /index.html?$args redirect;
}
location = /index.html {
if ($args = '') {
rewrite ^ /redirect.html last;
}
}
EOF
else
touch /etc/nginx/includes/wsqs_redirect.conf
fi fi
exec nginx -g 'daemon off;' exec nginx -g 'daemon off;'