【Nginx】url 帶有 “https://” 雙斜杠特殊處理


問題

最近發現經常有客戶端出現 url 復制兩次訪問的情況,導致觸發了 waf 無法訪問 403。所以就想着怎么處理,解決這個問題。

當用戶請求:https://www.test.com/https://www.test.com/或者https://www.test.com/https://www.test.com會出問題

原因

開啟壓縮之后,nginx 的 uri 會默認把雙斜桿//自動合並為單斜桿/

方法一:merge_slashes

啟用或禁用將URI中的兩個或多個相鄰斜杠壓縮為單個斜杠。處於安全考慮,一般不關閉。

merge_slashes off;

方法二:

if ( $uri ~ "http:(/)localhost(/|)$" ) {
    rewrite ^ $scheme://$host:$server_port/ break;
}  

方法三:

if ( $uri ~ "http:/(.*)localhost(/|)$" ) {
    rewrite ^ $scheme://$host:$server_port/ break;
}  

參考

http://nginx.org/en/docs/http/ngx_http_core_module.html#merge_slashes

https://stackoverflow.com/questions/38175840/match-and-redirect-http-double-slash-from-uri-path-with-nginx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM