我在使用 NAT 的虚拟机中运行 nginx,当我从主机访问它时遇到重定向问题。
按预期工作
-
http://localhost:8080/test/index.htm
: works.
-
http://localhost:8080/test/
: works.
没有按预期工作
-
http://localhost:8080/test
: 重定向到http://localhost/test/
。这是not我想要的是。(注意它删除了端口号)
我尝试过的
根据我用谷歌搜索的内容,我尝试过server_name_in_redirect off;
and rewrite ^([^.]*[^/])$ $1/ permanent;
,都没有成功。
我的default.conf:
server {
listen 80;
server_name localhost;
# server_name_in_redirect off;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
# rewrite ^([^.]*[^/])$ $1/ permanent;
root /usr/share/nginx/html;
try_files $uri =404;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
一个对我有用的更简单的解决方案是禁用绝对重定向absolute_redirect off;
如以下示例所示:
server {
listen 80;
server_name localhost;
absolute_redirect off;
location /foo/ {
proxy_pass http://bar/;
}
如果我运行curl onhttp://localhost:8080/foo
,我可以看到Location
重定向 HTTP 响应中的标头给出为/foo/
并不是http://localhost/foo/
.
$ curl -I http://localhost:8080/foo
HTTP/1.1 301 Moved Permanently
Server: nginx/1.13.8
Date: Tue, 03 Apr 2018 20:13:28 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: /foo/
由此看来,我认为任何网络浏览器都会根据相对位置执行正确的操作。在 Chrome 上测试,效果很好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)