Nginx 缺少尾部斜杠,导致代理重定向到错误的 URL

Nginx 缺少尾部斜杠,导致代理重定向到错误的 URL

我们在 nginx 代理后面使用 nginx 网络服务器。当我尝试浏览子路径时,nginx 后端网络服务器 (something.example.local) 会在 URL 后面附加一个尾部斜杠 (301 重定向) - 正如预期的那样 -。我的问题是,它重定向到错误的 URL,该 URL 不是由前端代理发布的,因此需要屏蔽。消除此问题的最佳解决方案是什么?

(我尝试了很多类似的建议,但都没有奏效:

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

相关内容