首页 > 解决方案 > nginx proxy_pass 在 url 中重写不保留原始路径

问题描述

我正在尝试设置 nginx 以通过以下方式转发传入请求:

http://localhost/service -> http://localhost:8080
http://localhost/service/foo -> http://localhost:8080/foo

现在我可以使用以下配置实现第一行:

...

upstream service {
    server service:8080;
}

... 

location /service/ {
    server_name_in_redirect off;
    proxy_pass         http://service;
    proxy_redirect     off;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
 }

但是当我添加额外的路径(例如 /foo)时,url 会以这种方式重写

http://localhost/foo/

因此我永远无法访问 localhost:8080/foo。关于如何使这项工作的任何想法?提前致谢

标签: nginxurl-rewritingreverse-proxy

解决方案


推荐阅读