NGINX如何根据SSO参数实现条件重定向及重复参数问题修复
NGINX重定向SSO参数无限追加问题解决方案
问题原因
原配置出现参数无限追加的核心原因有两个:
rewrite指令默认会将原请求的查询参数自动追加到目标地址末尾,导致每次重定向都会把已有的SSO=1再叠加一次- 全局写死的
rewrite ^/$ /?SSO=1 permanent;规则没有做参数判断,不管请求有没有携带参数都会触发重定向,造成无限循环
修复后的完整配置
server { listen 8080; index index.php index.html; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; port_in_redirect off; # 规则1:仅无参数访问根路径时,跳转到带SSO=1的地址 # 末尾加?表示不追加原请求的查询参数,避免参数叠加 if ($args = '') { rewrite ^/$ /?SSO=1? permanent; } # 规则2:携带SSO参数且值不为1时,跳转到无参数根路径 if ($arg_SSO != '1') { rewrite ^/$ /? permanent; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SERVER_NAME $host; } location ~ /\.ht { deny all; } location ~ php-errors\.log$ { deny all; } }
逻辑验证
- 无参数访问根路径:触发规则1,301重定向到
/?SSO=1,跳转后两个规则都不满足,循环终止 - 访问
/?SSO=1:两个规则都不触发,正常处理请求 - 访问
/?SSO=0或/?SSO=2等非1值:触发规则2,301重定向到无参数根路径,之后触发规则1跳转到/?SSO=1,符合需求
内容的提问来源于stack exchange,提问作者Roberta Soliman Donofreo
相关产品推荐
相关产品推荐

