Nginx rewrite模块配置

引言

在Nginx已经成为很多公司作为前端反向代理服务器的首选,在实际工作中往往会遇到很多跳转(重写URL)的需求。比如更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。如果在后端使用的Apache服务器,虽然也能做跳​转,规则库也很强大,但是用Nginx跳转效率会更高。

一、Rewrite简介

1.1、Rewrite跳转场景

1、URL看起来更规范、合理
2、企业会将动态URL地址伪装成静态地址提供服务
3、网址换新域名后,让旧的访问跳转到新的域名上
4、服务端某些业务调整

1.2、Rewrite跳转实现

1.3、Rewrite实际场景

(1)、Nginx跳转需求的实现方式
使用rewrite进行匹配跳转
使用if匹配全局变量后跳转
使用location匹配再跳转
(2)、rewrite放在server{},if{},location{}段中
location只对域名后边的除去传递参数外的字符串起作用
(3)、对域名或参数字符串
使用if全局变量匹配
使用proxy_pass反向代理

1.4、Nginx正则表达式

常用的正则表达式元字符
^:匹配输入字符串的起始位置
$:匹配输入字符串的结束位置
*****:匹配前面的字符零次或多次
+:匹配前面的字符一次或多次
?:匹配前面的字符零次或一次
.:匹配除\n之外的任何单个字符 使用[.\n]可以匹配包括\n在内的任意字符
****:转义符
\d:匹配纯数字
{n}:重复n次
{n,}:重复n次或更多次
[c]:匹配单个字符c
[a-z]:匹配a-z小写字母的任意一个
[a-zA-Z]:匹配a-z小写字母或A-Z大写字母的任意一个
():表达式的开始和结束位置
|:或运算符

二、Rewrite命令

2.1、Rewrite命令语法

 2.2、flag标记说明

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

2.3、last和break比较

1)、last:url重写后,马上发起一个新请求。再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏不变。
2)、break:url重写后,直接使用当前资源,不再使用location余下的语句,完成本次请求,地址栏不变。
总结:last和break在重定向后,地址栏都不会发生变化,这是它们的相同点,不同点在于last会写在server和if中,break是写在location中,last不会终止重写后的url匹配,break会终止重写后的url匹配。

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

三、location

3.1、location优先级

1、相同类型的表达式,字符串长的会优先匹配
2、按优先级排列(把最先执行的写在配置文件的最上面)
= 类型
^~ 类型表达式
正则表达式(和*)类型
常规字符串匹配类型,按前缀匹配
通用匹配(/),如果没有其它匹配,任何请求都会匹配到

3.2、rewrite和location比较

1、相同点
都能实现跳转
2、不同点
rewrite是在同一域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器
3、rewrite会写在location里,执行顺序
执行server块里面的rewrite指令
执行location匹配
执行选定的location中的rewrite指令
4、location优先级示例

location = / {	###精确匹配 /,主机名后面不能带任何字符串
    [configuraion A ]	
}

location / {	###所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配
    [configuraion B ]
}

location /documents/ {		###匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用
    [configuraion C ]
}

location ~ /documents/abc {		###匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用
    [configuraion D ]
}

location ^~ /images/ {	###以/images/开头的地址,匹配符合后,停止往下匹配
    [configuraion E ]
}

location ~*\.(gif|jpg|gpeg)$ {	###匹配所有以 gif, jpg或jpeg结尾的请求, Images/下的图片会被 [configuration E]处理,因为^~的优先级更高
    [configuraion F ]
}

location /images/abc {	###最长字符匹配到 /images/abc,优先级最低
    [configuraion G ]
}

location ~ /images/abc {	###以/ Images/abc开头的,优先级次之
    [configuraion H ]
}

location /images/abc/1.html {	###如果和正则 ~ images/abc/1.htm相比,正则优先级更高
    [configuraion I ]
}

3.3、location常用优先级规则

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

3.4 实际网站使用中,至少有三个匹配规则定义

(1)第一个必选规则

​直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网。
这里是直接转发给后端应用服务器了,也可以是一个静态首页​

location = / {
    proxy_pass http://tomcat_server/;
}

(2) 第二个必选规则

处理静态文件请求,这是nginx作为http服务器的强项
有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用​

location ^~ /static/ {
    root /webroot/static/;
}
 
location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

(3)第三个规则

通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器
非静态文件请求就默认是动态请求​

location / {
    proxy_pass http://tomcat_server;
}

3.5 匹配规则格式

location= patt {} [精准匹配]
location patt {} [一般匹配]
location ~ patt {} [正则匹配]
标记说明
~执行一个正则匹配,区分大小写
~*执行一个正则匹配,不区分大小写
!~执行一个正则匹配,区分大小写并取反(区分大小写不匹配)
!~*执行一个正则匹配,不区分大小写并取反(不区分大小写不匹配)
^~普通字符匹配:使用前缀匹配,如果匹配成功,则不在匹配其他location
=普通字符精确匹配,也就是完全匹配
@           定义一个命名的location,使用在内部命令时

四、应用案例

环境准备:安装Nginx服务

4.1、基于域名的跳转

现在公司旧域名www.old.com有业务需求有变更,需要使用新域名www.new.com代替。
要求:
旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变
1、修改默认站点配置文件

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
 server {
    listen       80;
    server_name  www.fbc.com;       #域名修改   
    charset utf-8;
    access_log  /var/log/nginx/fbc.access.log;      #日志修改
    location / {
    #添加域名重定向
        if ($host = 'www.fbc.com'){             #$host为rewrite全局变量,代表请求主机头字段或主机名
            rewrite ^/(.*)$ http://www.new.com/$1 permanent;     #$1为正则匹配的内容,即域名后边的字符串
        }
        root   html;
        index  index.html index.htm;
    }
}

2、修改主页内容

[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# vi /usr/local/nginx/html/test/index.html
<html><body><h1>this is new</h1></body></html>

3、重启服务

[root@localhost ~]# systemctl restart nginx

4、添加映射

[root@localhost ~]# vi /etc/hosts
192.168.154.19 www.fbc.com www.new.com

5、验证

在客户端输入www.old.com进行验证

 

4.2、基于客户端 IP 访问跳转

今天公司业务新版本上线,要求所有 IP 访问任何内容都显示一个固定维护页面,只有公司 IP 192.168.168.154访问正常。

(1)修改配置文件

vim /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  www.fbc.com;       #域名修改   
    charset utf-8;
    access_log  /var/log/nginx/fbc.access.log  main;        #日志修改
 
    #设置是否合法的IP标记
    set $rewrite true;                          #设置变量$rewrite,变量值为boole值true
    #判断是否为合法IP
    if ($remote_addr = "192.168.154.19"){       #当客户端IP为192.168.154.19时,将变量值设为false,不进行重写
        set $rewrite false;
    }
    #除了合法IP,其它都是非法IP,进行重写跳转维护页面
    if ($rewrite = true){                       #当变量值为true时,进行重写
        rewrite (.+) /weihu.html;               #重写在访问IP后边插入/weihu.html,例如192.168.154.20/weihu.html
    }
    location = /weihu.html {
        root /var/www/html;                     #网页返回/var/www/html/weihu.html的内容
    }
    
    location / {
        root   html;
        index  index.html index.htm;
    }

 2、创建存放维护网站的路径

mkdir -p /var/www/html/
echo 'weihuzhong' > /var/www/html/weihu.html
systemctl restart nginx

(3)验证结果

先用19主机访问

 然后用20主机访问

 4.3、基于旧域名跳转到新域名后面加目录 

现在访问的是 http://fbc.old.com,现在需要将这个域名下面的访问都跳转到http://www.old.com/fbc

(1)编辑配置文件

vim /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  www.fbc.com;      #域名修改   
    charset utf-8;
    access_log  /var/log/nginx/fbc.com.access.log;
    #添加
    location /post {
        rewrite (.+) http://www.fbc.com/abc$1 permanent;       #这里的$1为位置变量,代表/post
    }
    
    location / {
        root   html;
        index  index.html index.htm;
    }

2、更改主页

mkdir -p /usr/local/nginx/html/abc/post
systemctl restart nginx.service
echo "this is old" > /usr/local/nginx/html/abc/post/1.html



3、ip映射

echo "192.168.154.19 abc.fbc.com" >> /etc/hosts
systecmtl restart nginx

4、验证结果

浏览器输入http://abc.fbc.com/post/1.html 跳转到了www.fbc.com/post/1.html

 

 4.4、基于参数匹配的跳转

要求:
现在访问http://www.fbc.com/100-(200|300)-100.html
跳转到http://www.fbc.com页面。

1、修改配置文件

[root@fbc ~]# vi /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  www.fbc.com;       #域名修改   
    charset utf-8;
    access_log  /var/log/nginx/fbc.com.access.log;
    
    if ($request_uri ~ ^/100-(200|300)-(\d+).html$) {  #区分大小写以100-(200或者300)-一个或者多个数字 
        rewrite (.+) http://www.fbc.com permanent;     #只要匹配到以上条件就跳转到 www.fbc.com  permanent是永久重定向
    }
    location / {
        root   html;
        index  index.html index.htm;
    }
}

2、重启服务

systemctl restart nginx

3、验证结果

4.5、基于目录下所有 php 结尾的文件跳转 

要求访问 http://www.fbc.com/upload/123.php 跳转到首页。

(1)编辑配置文件

vim /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  www.fbc.com;       #域名修改   
    charset utf-8;
    access_log  /var/log/nginx/www.fbc.com-access.log;
    
location ~* /good/.*\.php$ {    #~*为不区分大小写 .*为任意字符  php结尾
    rewrite (.+) http://www.fbc.com permanent;
}
 
location / {
    root   html;
    index  index.html index.htm;
}
}

(2)浏览器访问

​http://www.fbc.com/good/123.php 跳转到http://www.fbc.com页面

4.6、基于最普通一条 url 请求的跳转

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

(1)编辑配置文件

vim /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
    server_name  www.fbc.com;       #域名修改   
    charset utf-8;
    access_log  /var/log/nginx/www.fbc.com.access.log;
    
    location ~* ^/abc/123.html {
        rewrite (.+) http://www.fbc.com permanent;
    }
 
    location / {
        root   html;
        index  index.html index.htm;
    }
}

(2)验证结果

​http://www.fbc.com/abc/123.html 跳转到www.fbc.com

五、总结

在生产环境中我们要掌握location的分类(精确匹配、一般匹配、正则匹配);掌握location的优先级(location = 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (location /);掌握rewrite的几种使用方法:基于域名的跳转、基于客户端 IP 访问跳转、基于旧域名跳转到新域名后面加目录、基于参数匹配的跳转、基于目录下所有 php 结尾的文件跳转、基于最普通一条 url 请求的跳转

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值