bed*_*dre 5 redirect reverse-proxy nginx
我正在尝试反向代理一个内部有 N 重定向 301 的 URL。
\n我想跟踪所有页面,然后反向代理最后一个页面(fourth-url.php)。
Nginx 有双重功能吗?
\n\n这是场景
\n\n第一个网址:
\n\nhttp://first-domain.com/first-url.php
\n\n重定向至
\n\nhttp://first-domain.com/second-url.php
\n\n重定向至
\n\nhttp://first-domain.com/third-url.php
\n\n重定向至(最后一个)
\n\nhttp://first-domain.com/fourth-url.php
\n\n这是我到目前为止所做的文件配置:
\n\nserver {\n\n set $base_url \'http://first-domain.com\';\n set $page \'first-url.php\'\n\n location /myserver {\n resolver 8.8.8.8;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $remote_addr;\n proxy_set_header Host $host;\n proxy_pass $base_url/$page;\n proxy_intercept_errors on;\n error_page 301 302 307 = @handle_redirect;\n }\n\n location @handle_redirect {\n resolver 8.8.8.8;\n set $saved_redirect_location $upstream_http_location;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $remote_addr;\n proxy_set_header Host $host;\n proxy_pass $base_url/$saved_redirect_location;\n proxy_intercept_errors on;\n error_page 301 302 307 = @handle_redirect;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这应该作为递归函数工作,但也许我缺少 if 语句或最后一部分。
\n\n实际上它的工作原理是这样的:
\n\nhttp://first-domain.com/first-url.php [确定]
\n http://first-domain.com/second-url.php [确定]
然后 nginx 停止一切并返回:
\n\nHTTP request sent, awaiting response... 302 Moved Temporarily\nLocation: unspecified\nERROR: Redirection (302) without location.\nRun Code Online (Sandbox Code Playgroud)\n\n旁注:first-url.php、second-url.php 等 \xe2\x80\xa6 仅包含一行简单的代码来进行重定向:
\n\nheader("Location: second-url.php");\nRun Code Online (Sandbox Code Playgroud)\n\n任何提示/想法表示赞赏。
\n小智 1
解决方案:添加 recursive_error_pages 指令。
server {
...
proxy_intercept_errors on;
recursive_error_pages on;
error_page 301 302 303 307 308 = @handle_redirect;
...
}
Run Code Online (Sandbox Code Playgroud)
另外,根据nginx recursion limit,你不必担心循环重定向问题,因为 nginx 将递归限制设置为 10。
| 归档时间: |
|
| 查看次数: |
8574 次 |
| 最近记录: |