我們在 nginx 代理程式後面使用 nginx 網路伺服器。當我嘗試瀏覽子路徑時,nginx 後端網頁伺服器(something.example.local)會在 url 後面附加一個尾部斜線(301 重定向) - 正如預期的那樣。我的問題是,它重定向到錯誤的網址,該網址不是由前端代理程式發布的,並且應該被封鎖。消除這個問題的最佳解決方案是什麼?
(我嘗試了很多類似的建議,但沒有一個有效:
https://serverfault.com/questions/1043091/unexpected-301-redirects-from-nginx-when-behind-nginx-reverse-proxy https://bluegrid.io/edu/how-to-add-a-trailing-slash-on-urls-in-nginx/ https://stackoverflow.com/questions/22759345/nginx-trailing-slash-in-proxy-pass-url https://www.shellhacks.com/nginx-proxy_pass-without-trailing-slash/ 絕對重定向關閉、代理重定向配置、後端伺服器上的重寫規則)
Nginx「前端」代理程式配置:
location /goodpath/ {
proxy_pass https://something.example.local/wrongpath/;
proxy_set_header Host example.com:$server_port;
proxy_set_header X-Real-IP $remote_addr;
}
# curl -I https://example.com/goodpath
HTTP/1.1 301 Moved Permanently
server: nginx
date: Thu, 31 Aug 2023 13:29:25 GMT
content-type: text/html
content-length: 162
location: **https://example.com/goodpath/**
$ curl -I **https://example.com/goodpath/subpath**
HTTP/1.1 301 Moved Permanently
server: nginx
date: Thu, 31 Aug 2023 13:30:23 GMT
content-type: text/html
content-length: 162
location: **https://example.com/*wrongpath*/subpath/**
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
referrer-policy: no-referrer-when-downgrade
permissions-policy: interest-cohort=()
strict-transport-security: max-age=31536000; includeSubDomains