Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redirect Nginx跟随重定向和反向代理最后位置_Redirect_Nginx_Reverse Proxy - Fatal编程技术网

Redirect Nginx跟随重定向和反向代理最后位置

Redirect Nginx跟随重定向和反向代理最后位置,redirect,nginx,reverse-proxy,Redirect,Nginx,Reverse Proxy,我正在尝试反向代理包含N重定向301的URL。 我希望跟踪所有页面,然后反向代理最后一个页面(第四个url.php) Nginx是否有双重功能 情况就是这样 第一个URL: 重定向到 重定向到 重定向到(最后一个) 这是我迄今为止所做的文件配置: server { set $base_url 'http://first-domain.com'; set $page 'first-url.php' location /myserver { resolver 8.8

我正在尝试反向代理包含N重定向301的URL。
我希望跟踪所有页面,然后反向代理最后一个页面(第四个url.php)

Nginx是否有双重功能

情况就是这样

第一个URL:

重定向到

重定向到

重定向到(最后一个)

这是我迄今为止所做的文件配置:

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语句或最后一部分

实际上它是这样工作的:

[确定]
[好的]

然后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");

欢迎任何提示/想法。

解决方案:添加递归错误页面指令

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

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

这个问题似乎类似,但将其扩展到4个重定向将非常混乱-在这种情况下,正确的解决方案是重写后端,而不使用太多的重定向:)