如何解决 Nginx 端口映射到外网后访问地址端口丢失的问题

2022-12-29,,,,

1. 问题说明


一个手机h5页面的项目,使用nginx(监听80端口)进行访问,内网访问的地址是192.168.12.125/h5,访问正常,nginx中的配置如下:

#微信H5页面访问
location /h5 {
alias /home/run/web/front;
index h5index.html;
break;
}

使用curl查看的信息如下:

root@ubuntu:~# curl -v 192.168.12.125/h5
* Trying 192.168.12.125...
* Connected to 192.168.12.125 (192.168.12.125) port 80 (#0)
> GET /h5 HTTP/1.1
> Host: 192.168.12.125
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Mon, 25 Nov 2019 02:29:54 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Location: http://192.168.12.125/h5/
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,OPTIONS,PUT,DELETE
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host 192.168.12.125 left intact

可以看到在访问的时候没有在uri的最后添加/,但是nginx会自动添加一个/,并且返回一个301重定向。如果当前nginx监听的是80端口,这个重定向行为不会影响页面的访问。但是如果nginx监听的是其他非80端口,或者是将nginx的80端口映射至外网的其他非80端口的时候,页面访问就会出现问题,例如将192.168.12.125服务器的80端口映射至公网IP地址 35.110.65.81:8888 端口并使用35.110.65.81:8888/h5进行访问,会发现地址被重定向为 35.110.65.81/h5/ 并且页面无法访问,使用curl查看信息如下:

[root@iZmkx0kvsmpmfvZ ~]# curl -v http://35.110.65.81:8888/h5
* About to connect() to 35.110.65.81 port 8888 (#0)
* Trying 35.110.65.81... connected
* Connected to 35.110.65.81 (35.110.65.81) port 8888 (#0)
> GET /h5 HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 35.110.65.81:8888
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Mon, 25 Nov 2019 02:14:57 GMT
< Content-Type: text/html
< Location: http://35.110.65.81/h5/
< Transfer-Encoding: chunked
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,OPTIONS,PUT,DELETE
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host 35.110.65.81 left intact
* Closing connection #0

2. 解决办法


对于这个问题的处理办法是在nginx中配置重写规则来添加端口信息,添加的配置如下:

#微信H5页面访问
location /h5 {
if (-d $request_filename) {
rewrite [^/]$ $scheme://$http_host$uri/ permanent;
}
alias /home/run/web/front;
index h5index.html;
break;
}

配置完成后重启nginx,并且使用curl进行测试,显示的信息如下:

[root@iZmkx0kvsmpmfvZ ~]# curl -v http://35.110.65.81:8888/h5
* About to connect() to 35.110.65.81 port 8888 (#0)
* Trying 35.110.65.81... connected
* Connected to 35.110.65.81 (35.110.65.81) port 8888 (#0)
> GET /h5 HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 35.110.65.81:8888
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Mon, 25 Nov 2019 02:16:03 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Location: http://35.110.65.81:8888/h5/
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,OPTIONS,PUT,DELETE
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host 35.110.65.81 left intact
* Closing connection #0

可以看到访问后的location地址为35.110.65.81:8888/h5/,页面也可以正常访问。

如何解决 Nginx 端口映射到外网后访问地址端口丢失的问题的相关教程结束。

《如何解决 Nginx 端口映射到外网后访问地址端口丢失的问题.doc》

下载本文的Word格式文档,以方便收藏与打印。