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

最新文章

  1. Python语法一
  2. js模板引擎
  3. PowerMock遇到的问题——4
  4. 【剑指Offer学习】【全部面试题汇总】
  5. poj2752Seek the Name, Seek the Fame
  6. IOC容器Unity的使用及独立配置文件Unity.Config
  7. Sqlserver 2005 跨数据库 导入数据
  8. [Storage]RPM series linux rescan disk / RPM系Linux重新扫描硬盘
  9. 转 MYSQL SELECT ... FOR UPDATE and SELECT ... LOCK IN SHARE MODE Locking Reads
  10. 【Pattern】-NO.150.Pattern.1 -【Pattern UML】
  11. 【linux】16进制格式查看命令hexdump
  12. libXext.so.6: cannot open shared object file:
  13. Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)
  14. 【bzoj5084】hashit 广义后缀自动机+树链的并+STL-set
  15. bzoj千题计划132:bzoj1189: [HNOI2007]紧急疏散evacuate
  16. open方法读写文件
  17. centos7虚拟机安装elasticsearch6.4.x-遇到的坑
  18. jq_常用开发模块
  19. 线段树lazy标记??Hdu4902
  20. c的详细学习(4)选择结构与循环结构的编程练习

热门文章

  1. (转)HttpServletResquest对象
  2. 聊聊经典数据结构HashMap,逐行分析每一个关键点
  3. 双向最大匹配算法——基于词典规则的中文分词(Java实现)
  4. 前端gitlab-ci.yml 入门
  5. 路由总结之静态、RIP、OSPF、IS-IS、BGP和策略路由
  6. 实验 3:Mininet 实验——测量路径的损耗率
  7. Spring Boot 第六弹,拦截器如何配置,看这儿~
  8. 【DFIR】数字取证与事件应急响应---初识
  9. matlab receive License Manager Error -103?
  10. C++中头文件简介(stdio.h &amp; chrono)