对于nginx,大家已经如雷贯耳。但大多数人像我一样只是停留在配置使用阶段。对于出现问题不知道如何定位问题,只能是百度,遵循前人的经验。或者打开官方网站查找。昨天我们上线就遇到了一个nginx配置不当引发301问题

问题描述:新系统上线完后,发现通过域名访问时而好用(mgmt.jerry.com/demo/)时而不好用(mgmt.jerry.com/demo).仔细观察请求地址发现不好用的时候项目名后面没有斜杠“/”。这是为什么呢?另外发现请求不带斜杠nginx默认301永久重定向到mgmt.jerry.com:8080/demo/ (nginx配置的监听端口是8080)端口号由80变成了8080

以下是我们nginx.conf的部分配置

       location ~ ^/(demo)/ {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location ~ ^/(demo1)/ {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location ~ ^/(demo2)/ {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location = / {proxy_pass  http://127.0.0.1:8080/demo;}

配置文件的意图很明显:不同的项目名过来命中不同的index页面,默认只访问域名代理到demo的index页面。

~ ^ 匹配输入字符串开始的位置,location的正则表达式中使用了目录/(demo)/,当在浏览器输入mgmt.jerry.com/demo是无法匹配上的。而mgmt.jerry.com/demo/是可以匹配上的,匹配上就可以正常访问了demo/index.html了。这就解释了有的时候好使,有时候不好使的问题了,立马修改配置为

  location ~ ^/(demo) {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location ~ ^/(demo1) {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location ~ ^/(demo2) {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location = / {proxy_pass  http://127.0.0.1:8080/demo;}

这样不管带不带斜杠都能正常匹配,心里美滋滋。不过仔细看配置就会发现还会有另一个问题:

假如我请求的地址是mgmt.jerry.com/demoabc,那照样也可以匹配到location ~ ^/(demo) ,并且正常访问。这肯定是不能容忍的。那有没有一个表达式机能满足业务需求又能将三个location合并成一个呢?

当然有,那就要用\w(匹配包括下划线的任何单词字符。等价于’[A-Za-z0-9_]’)了。我们的访问路径中规定只有字母、数字、下划线、斜杠。那使用\w就可以轻松解决了。后续即使有新项目接入,也可以无需修改表达式了。完整配置如下:

        location ~ ^/(\w+)/? {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location = / {proxy_pass  http://127.0.0.1:8080/demo;}

当你访问的项目名字不存在的时候,直接会返回404(前端会跳转到错误页面)

接下来就是访问域名或根目录了(mgmt.jerry.com),发现通过以上方式还是不OK的,没有达到想要的效果。跟前端小伙伴沟通发现前端本次上线删除了重定向,需要nginx协助实现重定向,定位了问题就好解决了。改成如下配置

        location ~ ^/(\w+)/? {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location = / {rewrite / /demo/ redirect;}

发现还是不ok,什么问题呢?跟一开始说的问题是一样的。域名http默认端口是80,而我们重定向后的端口是8080,通过域名:8080/demo/访问肯定是不OK的,那么该如何解决呢?

方案一:使用nginx的内置变量$host,即如下配置

 location ~ ^/(\w+)/? {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location = / {rewrite / http://$host/demo/ redirect;}

直接利用host重定向到默认不带端口号地址。

方案二:之所以重定向出现端口后变化,是跟nginx配置有关系的。参照http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect

nginx配置中有一参数port_in_redirect,默认是on。配合server_name_in_redirect

Enables or disables the use of the primary server name, specified by the server_name directive, in absolute redirects issued by nginx. When the use of the primary server name is disabled, the name from the “Host” request header field is used. If this field is not present, the IP address of the server is used.

The use of a port in redirects is controlled by the port_in_redirect directive

简单理解就是server_name_in_redirect默认是打开的。如果是打开的,重定向的时候会使用nginx.conf中配置的server_name,并且配合port_in_redirect返回端口号。如果server_name_in_redirect是off,则使用request中的host作为定向的host。

知道了这一点后就可以修改如下配置了

        location ~ ^/(\w+)/? {proxy_pass  http://127.0.0.1:8080/$1/index.html;}location = / {server_name_in_redirect off;port_in_redirect off;rewrite / /demo/ redirect;}

如果你只想重定向的端口不变,跳转地址使用server_name或者其他地址,可以采用如下配置:

    location = / {port_in_redirect off;rewrite / http://yourIP/demo/ redirect;}

接下来再说一下最开始说的301重定向后加了/问题,这个可以参照nginx说明

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:

location /user/ {proxy_pass http://user.example.com;
}location = /user {proxy_pass http://login.example.com;
}
大体意思是说:如果你只配置了带/的正则,并且使用了proxy_path等几个功能,那么当你不带/请求的时候,就会返回301,并且带上/,如果想避免这个问题,就要配置两个正则,一个带,一个不带。当然你也可以写正则表达式,将两种情况都匹配上解决哟。

nginx 301永久定向相关推荐

  1. 网站换服务器内页301永久定向,网站内页301重定向

    网站内页301重定向 内容精选 换一换 参见<CANN 软件安装指南>进行开发环境搭建,并确保开发套件包Ascend-cann-toolkit安装完成.该场景下ATC工具安装在" ...

  2. 详解301永久重定向实现方法

    301永久重定向对SEO无任何不好的影响,而且网页A的关键词排名和PR级别都会传达给网页B,网站更换了域名,表示本网页永久性转移到另一个地址,对于搜索引擎优化|SEO来说,给搜索引擎一个友好的信息,告 ...

  3. Nginx rewrite规则实现http跳转到https及301永久重定向

    Nginx rewrite规则实现http跳转到https及301永久重定向 环境准备: [root@ubuntu1804 ~]#curl https://linux2022.com -Ik HTTP ...

  4. Nginx 301重定向域名

    原文地址为: Nginx 301重定向域名 为何要使用301重定向 在网站建设中需要网页重定向的情况很多:如网页目录结构变动,网页重命名.网页的扩展名改变.网站域名改变等.如果不做重定向,用户的收藏和 ...

  5. nginx 301和302的区别与设置

    什么是301跳转 301跳转也叫301重定向,也叫301转向,也叫301永久重定向,是网站建设过程中的一个功能.一般用于2个域名指向同一个网站. 一般来说,利用跳转,对网站的排名不会有影响.但不会转移 ...

  6. Nginx 301 302重定向

    # 301 永久重定向到https server {server_name xxx.com;rewrite ^(.*)$ https://$host$1 permanent; } # 302 临时重定 ...

  7. php nginx 301重定向,nginx实现http协议301、302重定向

    最近在配合其他团队对网站进行seo方面的优化,其中建议需要对url进行大量301修改,基本就是将原来的较长的url重新定向到一个比较短的url,提高对搜索引擎的友好程度,如果发现你的网页从一个很精简的 ...

  8. php多个域名301重定向到主域名代码,Nginx 301和apache重定向域名规则方法(多个域名,单个域名)...

    实例:将所有topsem.com  topsem.cn的域名都跳转到www.topsem.com这个域名,避免泛解析,有利于SEO server { listen 80; server_name *. ...

  9. Golang 301永久重定向

    为什么80%的码农都做不了架构师?>>>    比如我要把www.taadis.com永久重定向到taadis.com //main.go package mainimport (& ...

最新文章

  1. Lucene.Net无障碍学习和使用:搜索篇
  2. 免费版的 IDEA 为啥不能使用 Tomcat ?
  3. python opencv二值化图像_python opencv,读取彩色图像,提取三通道,图像二值化,提取图像的边缘...
  4. 国产 YI Tunnel 收银机器人如何秒杀日本自助收银方案
  5. 2020-11-23(dll注入方法)
  6. 云存储服务器技术,​云存储技术构架-服务器运维
  7. sqlplus远程连接k8s集群部署的oracle
  8. 2012年A题葡萄酒的评价论文与代码
  9. Apache基础安装(一)
  10. python根据坐标点拟合曲线绘图
  11. js面向对象与java面向对象的区别,被坑了,js语法跟Java面向对象语法还是有区别的...
  12. 设计模式之单例模式-C++
  13. python 安装容易吗,Python安装的步骤操作其实是件很容易的事
  14. 编写GO的WEB开发框架 (十三): 配置文件读取
  15. [转]一阶自回归模型和二阶自回归模型
  16. eclipse编译Duet固件的完整过程
  17. QT项目负责人必须掌握的Ui设计师功能——Promote to !
  18. MATLAB画旋转曲面1
  19. 银行卡收单____对账__单边账
  20. Windows动态磁盘转换为基本盘

热门文章

  1. 视频录制与编辑架构设计
  2. DICOM文件即内部信息的读取
  3. linux tomcat文件夹,linux 查看tomcat 在哪个文件夹
  4. Siemens Simcenter FloEFD 2021.1.0 v5312 for PTC Creo
  5. C++挖掘程序本质(第三章C++其他语法-终)李明杰-M了个J 配套教材
  6. json格式的数据集中每一条数据5个选项随机去掉一个错误选项,剩余选项重新排序
  7. 素数判定与大数分解【Miller-rabin算法】【pollard-rho算法】
  8. 从源码探究双亲委派机制
  9. Python-玩转数据-requests库
  10. NECCS|全国大学生英语竞赛C类|词汇和语法|词汇题|21:03~21:53