nginx设置重定向跳转后 ip:[端口]/abc 变成 ip/abc 而报错404
nginx配置:
server {
listen 80;
server_name _;
client_max_body_size 300m;
absolute_redirect off;
location / {
root html;
index index.html index.htm;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
## abc目录
location /abc/ {
if (-d $request_filename) {
rewrite [^/]$ $scheme://$http_host$uri/ permanent;
}
proxy_pass http://localhost:8088/;
##由于规定了流量出口必须是80,所以得在80的server上代理出8088端口的abc项目
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_http_version 1.1;
proxy_read_timeout 3600s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
}
server {
listen 8088;
server_name _;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
charset utf-8;
location / {
root /opt/abc/;
index index.html index.htm;
autoindex_exact_size off;
autoindex_localtime on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
}
}
本地测试项目是否能正常访问:
http://192.168.1.22:8088 正常访问
http://192.168.1.22/abc 正常访问
当我需要将192.168.1.22:8088映射到外网ip:[port] 进行外网访问时报错。
假设外网ip=122.23.43.21
路由分配:122.23.43.21:5566 指向内网 --> 192.168.1.22:8088
也就是说,当我访问 122.23.43.21:5566/abc 时 可访问http://192.168.1.22:8088 项目
但是当我使用外网地址访问项目时:
http://122.23.43.21:5566/abc/ 项目正常访问
http://122.23.43.21:5566/abc 项目由
http://122.23.43.21:5566/abc 重定向为 http://122.23.43.21/abc/ 而导致报错404
也就是说在nginx.conf里,nginx把所有的ip:[port]都会自动跳转成ip:80 也就是默认重定向后是80端口,而外网ip的80端口是没有作映射的,所以404
此时我们需要知道,重定向错误,必定出在nginx上。所以需要从server里下手
在反向代理中添加 absolute_redirect off;
server {
listen 8088;
server_name _;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
charset utf-8;
location / {
root /home/web/zhuhai-forest-resources-management/;
index index.html index.htm;
autoindex off; ##防止ip:[port]/test 重定向请求后变成 ip/test而导致404
autoindex_exact_size off;
autoindex_localtime on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
}
}
即可成功访问
参考如下文章:
nginx重定向导致端口消失无法正常访问
场景
访问路径带端口http://ip:端口/login,回车后端口消失http://ip/login无法正常访问
解决方法
nginx配置中中加一句:
新版本nginx(≥1.11.8)
absolute_redirect off;
旧版本nginx(<1.11.8)
port_in_redirect off;
番外:
1. 关于nginx自动跳转
站点目录结构:
根目录:/var/wwwroot/test/
当访问地址为 http://www.test.com/abc 时,因为nginx未找到abc这个文件,而是找到了abc目录,自动在地址后面加上了斜杠,所以自动发生 301 跳转到 http://www.test.com/abc/
2. nginx自动跳转相关的3个配置
Nginx中存在一三个跳转的相关配置:
1. absolute_redirect
Syntax: absolute_redirect on | off;Default: absolute_redirect on;Context: http, server, locationThis directive appeared in version 1.11.8.If disabled, redirects issued by nginx will be relative.See also server_name_in_redirect and port_in_redirect directives.
默认为开启。
2. server_name_in_redirect
Syntax: server_name_in_redirect on | off;Default: server_name_in_redirect off;Context: http, server, locationEnables or disables the useof the primary server name, specified by the server_name directive, in absolute redirects issued by nginx. When the useof the primary server name is disabled, the name from the “Host” request header field is used. If this field isnotpresent, the IP addressof the server is used.The use of a port in redirects is controlled by the port_in_redirect directive.
默认关闭
3. port_in_redirect
Syntax: port_in_redirect on | off;Default: port_in_redirect on;Context: http, server, locationEnables or disables specifying the port in absolute redirects issued by nginx.The use of the primary server name in redirects is controlled by the server_name_in_redirect directive.
默认开启。
3. 举例
访问地址为:http://192.168.1.232/abc
Nginx情况一:
server { listen 80; server_name test.com; absolute_redirect off; // 关闭了它,不管 server_name_in_redirect, port_in_redirect 怎么设置都没有用 server_name_in_redirect on; port_in_redirect on;}
发生 301 跳转,跳转后的地址为:http://192.168.1.232/abc/
官方说是使用 请求头中的 Host 字段,作为url跳转后host部分
Nginx情况二:
server { listen 80; server_name test.com; absolute_redirect on; // 默认为on, Nginx配置中没有这个字段其实值是on, server_name_in_redirect, port_in_redirect 会根据配置起作用 server_name_in_redirect on; // 发生跳转时使用 server 中配置的域名 port_in_redirect on; // 发生跳转时使用 server 中配置的端口}
发生 301 跳转,跳转后的地址为:http://test.com/abc/