【问题标题】:Rewrite URL for reverse proxy重写反向代理的 URL
【发布时间】:2014-03-24 16:20:08
【问题描述】:

这类似于nginx url rewrite for reverse proxy,但我不知道应该如何将这个答案应用于我的情况。

我正在尝试重写反向代理的 URL,以便在 Nginx 向上游服务器发出请求时,/pathA/something 变为 /pathB/something

location ~ /pathA(/|$) {
    proxy_pass http://www.example.com;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;

    rewrite ^/pathA(.*) /pathB$1;
}

我试过这个,但我得到一个 Nginx 404 页面(不是 example.com 404 页面)。

【问题讨论】:

  • 我不知道这是否有区别,但我认为重写应该在位置块的顶部。

标签: regex nginx rewrite reverse-proxy


【解决方案1】:

我需要添加重定向(302)或永久(301):

location ~ /pathA(/|$) {
    proxy_pass http://www.example.com;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;

    rewrite ^/pathA(.*) /pathB$1 permanent;
}

【讨论】:

    猜你喜欢
    • 2014-02-01
    • 1970-01-01
    • 2019-08-27
    • 2020-09-05
    • 2014-03-17
    • 2019-07-05
    • 2013-01-28
    • 1970-01-01
    • 2020-07-29
    相关资源
    最近更新 更多