Nginx Rewrite 重写跳转

前言

在工作中使用Nginx,往往会遇到很多需要跳转的需求。比如:当公司的服务器需要维护时,我们就可以用跳转功能让客户先从原先的域名转到新建的域名来提醒用户正在维护。

一、Nginx Rewrite概述

1.Rewrite 跳转场景

  • 调整用户浏览的URL(按照标准化流程走),看起来更规范更合理。
  • 为了让搜索引擎搜寻网站内容及用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
  • 网址换新域名后,让旧的访问跳转到新的域名上。像你访问原来的地址,出现的域名不是原来的域名。
  • 服务端某些业务调整,比如根据特殊变量、目录、客户端的信息进行URL调整等。

2. Rewrite 跳转实现

  • Nginx:通过ngx_http_rewrite_module 模块支持URL重写、支持if条件判断,但不支持else
  • 跳转:从一个location跳转到另一个location, 循环最多可以执行10次,超过后nginx将返回500错误
  • PCRE支持:perl兼容正则表达式的语法规则匹配
  • 重写模块 set 指令:创建新的变量并设其值

3. Rewrite 实际场景

  1. Nginx 跳转需求的实现方式:
  • 使用 rewrite 进行匹配跳转
  • 使用 if 匹配全局变量后跳转
  • 使用 location 匹配再跳转
  1. rewrite 放在 server{},if{},location{} 段中
  • location 只对域名后边的除去传递参数外的字符串起作用——页面文件的路径
  1. 对域名或参数字符串
  • 使用 if 全局变量匹配
  • 使用 proxy_pass 反向代理

二、Nginx常用的正则表达式符号

符号描述
^匹配输入字符申的起始位置
$匹配输入字符串的结束位置
*匹配前面的字符零次或多次
+匹配前面的字符一次或多次
匹配前面的字符零次或一次
.匹配除"\n"之外的任何单个字符
\将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用
\d匹配纯数字
{n}重复n次
{n,}重复n次或更多次
{n,m}重复n到m次
[]定义匹配的字符范围
[c]匹配单个字符
[a-z]匹配a-z小写字母的任意一个
[a-zA-Z0-9]匹配所有大小写字母或数字

三、Rewrite 命令详解

1.Rewrite语法

rewrite <regex> <replacement> [flag];

#<regex>:正则表达式        
#<replacement>:跳转后的内容     
#[flag]:rewrite支持的flag标记

flag标记说明

flag标记
标记说明
last相当于Apache的[L]标记,表示完成rewrite
break本条规则匹配完成即终止,不再匹配后面的任何规则
redirect返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url
permanent返回301永久重定向,浏览器地址会显示跳转后的URL地址,爬虫更新url

last和break比较

lastbreak
使用场景一般写在 server 和 if 中一般使用在 location 中
URL匹配不终止重写后的 urI 匹配终止重写后的 url 匹配

四、location 解析

1. location 分类

location = patt {}  [精准匹配]   			 #精确匹配字符串
location patt {}    [一般匹配]				 #只要包含patt的字符串即可
location ~ patt {}  [正则匹配]				 #按照正则表达式的方式匹配

2.正则匹配的常用表达式

标记说明
~执行一个正则匹配,区分大小写
~*执行一个正则匹配,不区分大小写
!~执行一个正则匹配,区分大小写不匹配
!~*执行一个正则匹配,不区分大小写不匹配
^~普通字符匹配,使用前缀匹配,如果匹配成功,则不再匹配其他location
=普通字符精确匹配,也就是完全匹配
@定义一个命名的location,使用在内部定向时

3.优先级

  • 相同类型的,字符串长的优先
  • 按优先级排列
= 类型                                     		#首先精确匹配
^~ 类型表达式								  		#其次前缀匹配
正则表达式(~和~*)类型					        #其次是按文件中顺序的正则匹配
常规字符串匹配类型,按前缀匹配				   	    #然后匹配不带任何修饰的前缀匹配	
通用匹配(/),如果没有其他匹配,任何请求都会匹配到 		#最后交给 / 通用匹配

4.比较localtion和rewrite

相同点:都能实现跳转
不同点:
rewrite 是在同一域名内更改获取资源的路径 (本地节点)
location是对一类路径控制访问或反向代理,还可以 proxy_pass 到其他机器(跨节点)
  • rewrite会写在location里,执行顺序
 执行server块里面的rewrite指令
 执行location匹配
 执行选定的location中的rewrite指令

5location优先级是怎么排列的?

  • 匹配某个具体文件
(location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径) > (location ~ 完整路径)> (location /)
  • 用目录做匹配访问某个文件
(location = 目录) > (location ^~ 目录) > (location ~ 目录) > (location ~* 目录)> (location /)

6.文件 目录 为什么只会在区不区分大小写上会有变动

正则表达式:目的是为了尽量精确的匹配
文件——》尽量精确匹配,区分大小写 精确、不区分更为精确
目录——》尽量精确匹配,区分大小写 精确 优先级更高,不区分大小写的

五、location 示例说明

1、location = / {}
= 为精确匹配 /,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配
再比如 location = /abc,则只匹配 /abc,/abc/或 /abcd不匹配。若 location /abc ,则即匹配/abc、/abcd/ 同时也匹配/abc/。
 
2、location / {} www.kgc.com/路径
因为所有的地址都以/开头,所以这条规则将匹配到所有请求比如访问/和/data, 则/匹配,/data 也匹配,
但若后面是正则表达式会和最长字符串优先匹配(最长匹配)

3、location /documents/ {}
匹配任何以/documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它location
只有其它location后面的正则表达式没有匹配到时,才会采用这一条

4、location /documents/abc {}
匹配任何以/documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它location
只有其它location后面的正则表达式没有匹配到时,才会采用这一条

5、location ^~ /images/ {}
匹配任何以/images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条

6、location ~* \. (gif|jpg|jpeg)$ {}
匹配所有以gif、jpg或jpeg结尾的请求
然而,所有请求/images/下的图片会被location ^~ /images/ 处理,因为^~的优先级更高,所以到达不了这一条正则
 
7、location /images/abc {}
最长字符匹配到/images/abc,优先级最低,继续往下搜索其它location, 会发现^~和~存在

8、location ~ /images/abc {}
匹配以/images/abc开头的,优先级次之,只有去掉location ^~  /images/ 才会采用这一条

9、location /images/abc/1.html {}
匹配/images/abc/1.html文件,如果和正则location ~ /images/abc/1.html相比,正则优先级更高
  • 在实际网站使用中,至少要有三个匹配规则定义

#第一个必选规则: 直接匹配网站根,通过域名访问网站首页比较频繁(www.baidu.com/),使用这个会加速处理,比如说官网;
可以是一个静态首页,也可以直接转发给后端应用服务器
location = / {
	root	html ;
	index index.html index.htm;
}
-------------------------------------------------------------------------------------------------
#第二个必选规则:处理静态文件请求,这是nginx作为http服务器的强项;
有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
	root /webroot/static/ ;
}
location ~* \. (html|gif|jpg|jpeg|png)$ {
	root /webroot/res/ ;
} 
------------------------------------------------------------------------------------------------
#第三个必选规则就是通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器,
非静态文件请求就默认是动态请求,即跳转或反向代理
upstream	tomcat_ server {
	192.168.8.135:80
	192.168.8.132:80
location / {
	proxy_ pass http://tomcat_server;
}
------------------------------------------------------------------------------------------------

六、案例

1. 基于域名的跳转

公司旧域名 www.gy.com 因业务需求的变更,需要使用新域名 www.gy1.com 代替,但是旧域名不能废除,并且旧域名要可跳转到新的域名上,而且后面的参数保持不变。

  • 添加映射,开启 nginx 服务
[root@localhost ~]#vim /etc/hosts
......
192.168.8.135 www.gy.com www.gy1.com
[root@localhost ~]#mkdir -p /var/log/nginx
[root@localhost ~]#vim /usr/local/nginx/conf/nginx.conf
......
server {
listen       80;
server_name  www.gy.com;                  		 #修改域名
charset utf-8;									 #字符集可选择性修改
access_log  /var/log/nginx/www.gy.com.log; 	 #开启并对日志保存路径进行修改
location / {									 #在原有的location位置进行插入下面内容
if ($host = 'www.gy.com'){						 #$host为rewrite全局变量,表示请求主机头字段或主机名
rewrite ^/(.*)$ http://www.gy1.com/$1 permanent;
}                    							 #$1为匹配的位置变量,即域名后面的字符串,同时永久跳转
root   html;
index  index.html index.htm;
}
......

[root@localhost ~]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]#systemctl restart nginx.service 
  • 用www.gy.com域名访问,显示的却是www.gy1.com

在这里插入图片描述

2. 基于 IP 地址访问跳转

由于公司的业务新版本上线,要求所有 IP 访问任何内容都显示一个固定的维护页面,只有公司自己的IP地址 192.168.74.131 才能正常访问。

创建维护页面的目录,并添加内容

[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim weihu.html
[root@localhost html]# cat weihu.html 
网站维护中。。。。。

主配置文件配置

vim /usr/local/nginx/conf/nginx.conf
......
server {
listen       80;
server_name  www.gkd.com;
charset utf-8;
access_log  /var/log/nginx/www.gkd.com.log;      #开启并对日志保存路径进行修改
set $rewrite true;								 #设置变量$rewrite,变量值为布尔值为true
if ($remote_addr = "192.168.74.131"){	#当客户端ip为192.168.74.131时,将变量值设为flase,不进行重写
set $rewrite false;
}										#除了合法IP,其他都是非法IP,进行重写跳转到维护页面
if ($rewrite = true){                   #布尔值表达式在不满足false情况下,会匹配满足true的location
rewrite (.+) /weihu.html;				#重写在访问IP后边加入/weihu.html,如192.168.8.135/weihu.html
}
location = /weihu.html {
root /var/www/html;						#页面返回/var/www/html/weihu.html里面的内容
}
location / {
root   html;
index  index.html index.htm;
}
......

检测配置文件语法,重启服务

[root@localhost /usr/local/nginx/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /usr/local/nginx/html]#systemctl restart nginx.service 

打开浏览器进行验证
此时用另一台机器进行访问192.168.74.131这个地址,会发现页面进行了跳转,跳转到维护的页面,只有IP地址为192.168.74.131的本机才可正常访问

3. 基于旧域名跳转到新域名后面加目录

要求:当访问的域名为 http://bbs.gkd.com/post/1.html 时自动跳转到 http: //www.haha.com/bbs/post/1.html

创建一个bbs目录下post的网页文件,并添加内容

[root@localhost /var/www/html]#mkdir -p /usr/local/nginx/html/bbs/post
[root@localhost /var/www/html]#vim /usr/local/nginx/html/bbs/post/1.html
[root@localhost /var/www/html]#cat /usr/local/nginx/html/bbs/post/1.html 
<h1>文档有毒~~~~~~</h1>
[root@localhost /var/www/html]#vim /etc/hosts
192.168.8.135 bbs.gkd.com www.haha.com

主配置文件配置

.......
server {
listen       80;
server_name  bbs.gkd.com;								#更改域名					
charset utf-8;
access_log  /var/log/nginx/www.gkd.com-access.log;      #开启并对日志保存路径进行修改
location /post {										#添加location;以post开头匹配
rewrite (.+) http://www.haha.com/bbs$1 permanent;       #$1为位置变量,代表/post
}
location / {
root   html;
index  index.html index.htm;
}
......

检查主配置文件语法并重启服务

[root@localhost /var/www/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /var/www/html]#systemctl restart nginx.service 

打开浏览器进行验证
可以看到此时域名已经自动跳转到了 http: //www.haha.com/bbs/post/1.html

4. 基于参数匹配(多余)的跳转

要求:访问 http://www.gkd.com/100-(100|200)-100.html 会跳转到 http://www.gkd.com的页面

主配置文件配置

[root@localhost /var/www/html]#vim /usr/local/nginx/conf/nginx.conf
......
server {
listen       80;
server_name  www.gkd.com;							#更改域名
charset utf-8;
access_log  /var/log/nginx/www.gkd.com-access.log;  #开启并对日志保存路径进行修改
#设置正则匹配,例如:http://www.gkd.com/100-100-123.html													
if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$){   #$repuest_uri内置变量,表示uri,\d 纯数字
rewrite (.*) http://www.gkd.com permanent;			#设置重写
}
location / {
root   html;
index  index.html index.htm;
}
......

检查主配置文件语法并重启服务,配置映射

[root@localhost /var/www/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /var/www/html]#systemctl restart nginx.service 
[root@localhost /var/www/html]#vim /etc/hosts
192.168.8.135 www.gkd.com

使用浏览器进行验证

可以看到访问域名 http://www.gkd.com/100-100-100.html 时直接跳转到了 http://www.gkd.com 的页面
访问 http://www.gkd.com/100-200-100.html 也可跳转

5. 基于目录下所有 php 结尾的文件跳转

要求:访问 http://www.gkd.com/upload/123.php 跳转到首页(常用场景:网站新用户提示需要注册)

主配置文件配置

[root@localhost /var/www/html]#vim /usr/local/nginx/conf/nginx.conf
......
server {
listen       80;
server_name  www.gkd.com;
charset utf-8;
access_log  /var/log/nginx/www.gkd.com-access.log;
location ~* /upload/.*\.php$ {         
# ~* 表示正则匹配,不区分大小写,匹配/upload目录下所有以.php结尾的文件
rewrite (.*) http://www.gkd.com permanent;
}
location / {
root   html;
index  index.html index.htm;
}
......

检查主配置文件语法并重启服务

[root@localhost /var/www/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /var/www/html]#systemctl restart nginx.service 

打开浏览器进行验证

可以看到访问 http://www.gkd.com/upload/123.php 会跳转到 http://www.gkd.com 首页

6. 基于最普通一条 url 请求的跳转

要求:访问一个具体的页面,如: http://www.gkd.com/abc/123.html,能跳转到首页

主配置文件配置

[root@localhost /var/www/html]#vim /usr/local/nginx/conf/nginx.conf
......
    server {
        listen       80;
        server_name  www.gkd.com;
        charset utf-8;
        access_log  /var/log/nginx/www.gkd.com-access.log;
        location ~* /abc/123.html {                     
                rewrite (.+) http://www.gkd.com permanent;
        }
        location / {
            root   html;
            index  index.html index.htm;
        }

......

检查主配置文件语法并重启服务

[root@localhost /var/www/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /var/www/html]#systemctl restart nginx.service

使用浏览器进行验证

总结

  • 1.比较rewrite和location
    相同点:都实现跳转
    不同点:rewrite是在同一域名内更改获取资源的路径

  • 2.location优先级是怎么排列的?
    匹配某个具体文件
    (location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径) > (location ~ 完整路径)> (location /)
    用目录做匹配访问某个文件
    (location = 目录) > (location ^~ 目录) > (location ~ 目录) > (location ~* 目录)> (location /)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值