我对 nginx 1.4.1 使用以下配置:
server {
listen 8000;
server_name correct.name.gr;
location /test/register {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1;
}
}
我想做的是当用户访问时http://correct.name.gr:8000/test/register/
它们应该被代理到在端口 80 上运行的 apache。
当我访问时http://correct.name.gr:8000/test/register/
我得到正确的结果(index.php)。
当我访问时http://correct.name.gr:8000/test/register/asd
我得到了正确的结果(来自 apache 的 404)。
当我访问时http://correct.name.gr:8000/test/asd
我得到了正确的结果(来自 nginx 的 404)。
当我访问时http://correct.name.gr:8000/test/register123
我得到了正确的结果(来自 apache 的 404)。
问题是当我访问时http://correct.name.gr:8000/test/register
。我收到 301 响应并被重定向到http://localhost/test/register/
(注意尾部斜杠,当然还有“localhost”)!!!
我没有对 nginx 进行任何其他配置来添加尾部斜杠或类似的内容。你知道问题是什么吗?我想http://correct.name.gr:8000/test/register
通过代理到 apache 来正常工作(或者如果不可能,至少发出 404 错误而不是重定向到用户的本地主机)。
Update 1: 我试过http://correct.name.gr:8000/test/register
来自与我昨天出现不良行为的计算机不同的计算机.. 好吧,它起作用了:我刚刚收到 301 响应,该响应向我指出了正确的位置http://correct.name.gr:8000/test/register/
!怎么可能在一台计算机上工作,但在另一台计算机上却不能工作(我在两台计算机上使用相同的浏览器 Chrome)?明天我将再次尝试从第三个测试中观察其行为。
谢谢 !
我的猜测是你的上游服务器(apache 或你的脚本)触发了重定向到absolute url http://localhost/test/register/
。因为你使用http://127.0.0.1
在你的proxy_pass
指令,nginx 没有找到匹配的域名并返回Location
标题原样。
我认为正确的解决方案是如果重定向到内部 url,则不要使用绝对重定向。这始终是一个很好的做法。
然而,在不改变上游服务器的情况下,有两种快速解决方案。
您可以使用
proxy_pass http://localhost;
这将告诉nginx上游的域名是localhost
。然后nginx就会知道要替换http://localhost
by http://correct.name.gr:8000
当它在中找到该部分时Location
来自上游的标头。
另一种是添加一个proxy_redirect
强制 nginx 重写任何位置标头的行http://localhost/
in it.
proxy_pass http://127.0.0.1;
proxy_redirect http://localhost/ /;
我更喜欢第一个解决方案,因为它更简单。使用时没有 DNS 查找开销proxy_pass http://localhost;
因为 nginx 在启动 Web 服务器时会提前进行查找。
参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)