nginx設(shè)置重定向跳轉(zhuǎn)后??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/;?
? ? ? ? ? ? ##由于規(guī)定了流量出口必須是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?正常訪問
當(dāng)我需要將192.168.1.22:8088映射到外網(wǎng)ip:[port]?進行外網(wǎng)訪問時報錯。
假設(shè)外網(wǎng)ip=122.23.43.21
路由分配:122.23.43.21:5566??指向內(nèi)網(wǎng)??-->?192.168.1.22:8088
也就是說,當(dāng)我訪問?122.23.43.21:5566/abc?時?可訪問http://192.168.1.22:8088 項目
但是當(dāng)我使用外網(wǎng)地址訪問項目時:
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/ ??而導(dǎo)致報錯404
也就是說在nginx.conf里,nginx把所有的ip:[port]都會自動跳轉(zhuǎn)成ip:80?也就是默認(rèn)重定向后是80端口,而外網(wǎng)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而導(dǎo)致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重定向?qū)е露丝谙o法正常訪問
場景
訪問路徑帶端口http://ip:端口/login,回車后端口消失http://ip/login無法正常訪問
解決方法
nginx配置中中加一句:
新版本nginx(≥1.11.8)
absolute_redirect off;
舊版本nginx(<1.11.8)
port_in_redirect off;
![nginx設(shè)置重定向跳轉(zhuǎn)后ip:[端口]/abc變成ip/abc而報錯404](https://imgs.yssmx.com/Uploads/2023/06/474148-1.png)
番外:
1. 關(guān)于nginx自動跳轉(zhuǎn)
站點目錄結(jié)構(gòu):
根目錄:/var/wwwroot/test/
![nginx設(shè)置重定向跳轉(zhuǎn)后ip:[端口]/abc變成ip/abc而報錯404](https://imgs.yssmx.com/Uploads/2023/06/474148-2.png)
當(dāng)訪問地址為 http://www.test.com/abc 時,因為nginx未找到abc這個文件,而是找到了abc目錄,自動在地址后面加上了斜杠,所以自動發(fā)生 301 跳轉(zhuǎn)到 http://www.test.com/abc/
2. nginx自動跳轉(zhuǎn)相關(guān)的3個配置
Nginx中存在一三個跳轉(zhuǎn)的相關(guān)配置:
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.
默認(rèn)為開啟。
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.
默認(rèn)關(guān)閉
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.
默認(rèn)開啟。
3. 舉例
訪問地址為:http://192.168.1.232/abc
Nginx情況一:
server { listen 80; server_name test.com; absolute_redirect off; // 關(guān)閉了它,不管 server_name_in_redirect, port_in_redirect 怎么設(shè)置都沒有用 server_name_in_redirect on; port_in_redirect on;}
發(fā)生 301 跳轉(zhuǎn),跳轉(zhuǎn)后的地址為:http://192.168.1.232/abc/
官方說是使用 請求頭中的 Host 字段,作為url跳轉(zhuǎn)后host部分 文章來源:http://www.zghlxwxcb.cn/news/detail-474148.html
![nginx設(shè)置重定向跳轉(zhuǎn)后ip:[端口]/abc變成ip/abc而報錯404](https://imgs.yssmx.com/Uploads/2023/06/474148-3.png)
Nginx情況二:
server { listen 80; server_name test.com; absolute_redirect on; // 默認(rèn)為on, Nginx配置中沒有這個字段其實值是on, server_name_in_redirect, port_in_redirect 會根據(jù)配置起作用 server_name_in_redirect on; // 發(fā)生跳轉(zhuǎn)時使用 server 中配置的域名 port_in_redirect on; // 發(fā)生跳轉(zhuǎn)時使用 server 中配置的端口}
發(fā)生 301 跳轉(zhuǎn),跳轉(zhuǎn)后的地址為:http://test.com/abc/文章來源地址http://www.zghlxwxcb.cn/news/detail-474148.html
到了這里,關(guān)于nginx設(shè)置重定向跳轉(zhuǎn)后ip:[端口]/abc變成ip/abc而報錯404的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!