【问题标题】:Nginx follow redirect and reverse proxy last locationNginx 跟随重定向和反向代理最后位置
【发布时间】:2017-06-20 15:30:20
【问题描述】:

我正在尝试反向代理内部具有 N 重定向 301 的 URL。
我想关注所有页面,然后反向代理最后一页(fourth-url.php)。

Nginx 有什么可加倍的吗?

这是场景

第一网址:

http://first-domain.com/first-url.php

重定向到

http://first-domain.com/second-url.php

重定向到

http://first-domain.com/third-url.php

重定向到(最后一个)

http://first-domain.com/fourth-url.php

这是我到目前为止所做的文件配置:

server {

  set $base_url 'http://first-domain.com';
  set $page 'first-url.php'

  location /myserver {
     resolver 8.8.8.8;
     proxy_set_header X-Real-IP  $remote_addr;
     proxy_set_header X-Forwarded-For $remote_addr;
     proxy_set_header Host $host;
     proxy_pass $base_url/$page;
     proxy_intercept_errors on;
     error_page 301 302 307 = @handle_redirect;
  }

  location @handle_redirect {
     resolver 8.8.8.8;
     set $saved_redirect_location $upstream_http_location;
     proxy_set_header X-Real-IP  $remote_addr;
     proxy_set_header X-Forwarded-For $remote_addr;
     proxy_set_header Host $host;
     proxy_pass $base_url/$saved_redirect_location;
     proxy_intercept_errors on;
     error_page 301 302 307 = @handle_redirect;
  }
}

这应该作为递归函数工作,但也许我缺少一个 if 语句或最后一部分。

其实是这样的:

http://first-domain.com/first-url.php [确定]
http://first-domain.com/second-url.php [确定]

然后 nginx 停止一切并返回:

HTTP request sent, awaiting response... 302 Moved Temporarily
Location: unspecified
ERROR: Redirection (302) without location.

旁注:first-url.php、second-url.php 等……只包含一行简单的代码来进行重定向:

header("Location: second-url.php");

任何提示/想法表示赞赏。

【问题讨论】:

  • 这个问题看起来很相似,尽管将其扩展为 4 个重定向会很麻烦 - 在这种情况下,正确的解决方案是重写后端而不使用那么多重定向 :) serverfault.com/questions/423265/…跨度>

标签: redirect nginx reverse-proxy


【解决方案1】:

解决方案:添加 recursive_error_pages 指令。

server {
...
proxy_intercept_errors on;
recursive_error_pages on;
error_page 301 302 303 307 308 = @handle_redirect;
...
}

另外,根据nginx recursion limited,你不必担心循环重定向问题,因为nginx将递归限制设置为10。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2016-12-26
    • 2019-03-14
    • 1970-01-01
    • 2019-03-12
    • 2015-11-29
    • 1970-01-01
    相关资源
    最近更新 更多