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

推荐阅读

Helm3(K8S 资源对象管理工具)视频教程:https://edu.csdn.net/course/detail/32506
Helm3(K8S 资源对象管理工具)博客专栏:https://blog.csdn.net/xzk9381/category_10895812.html

本文原文链接:https://blog.csdn.net/xzk9381/article/details/109535875,转载请注明出处。如有发现文章中的任何问题,欢迎评论区留言。

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/,页面也可以正常访问。

本文原文链接:https://blog.csdn.net/xzk9381/article/details/109535875,转载请注明出处。如有发现文章中的任何问题,欢迎评论区留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

店伙计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值