You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx配置异常:特定路径转发至容器时被重定向至根路径

问题诊断与解决

核心原因

请求被重定向到根目录的本质是Nginx转发给special-container的路径不匹配:

  • 你的location /special_path_1/配置中,proxy_pass http://special-container:200;未以斜杠结尾,Nginx会把完整匹配路径/special_path_1/附加到目标URL后,转发请求到http://special-container:200/special_path_1/。
  • 但你在容器内执行curl http://special-container:200能正常响应,说明special-container的服务仅监听根路径(/),无法处理/special_path_1/路径的请求,因此返回了重定向(307)或404,最终Nginx将响应中的Location头转换为站点根路径。

修复方案

修改special_path_1和special_path_2的proxy_pass配置,在目标URL末尾添加斜杠:

location /special_path_1/ {
    proxy_redirect off;
    proxy_pass http://special-container:200/;  # 末尾添加斜杠
    proxy_http_version 1.1;
    proxy_set_header Host $host;
}

location /special_path_2/ {
    proxy_redirect off;
    proxy_pass http://special-container:201/;  # 末尾添加斜杠
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

添加斜杠后,Nginx会去掉请求路径中匹配/special_path_1/的部分,仅转发后续内容(此处为空)到容器的根路径,与你在容器内测试的请求逻辑一致。

验证步骤

修改配置后,重新加载Nginx:

nginx -s reload

再次访问https://example.com/special_path_1,即可正常获取special-container的响应。

内容的提问来源于stack exchange,提问作者user21489412

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 18:14:50