Nginx--正则匹配--实现rewrite跳转及应用示例


一、Nginx正则匹配

1.正则表达式

1.1 概述

  • 复杂的路径匹配需要使用正则表达式表示,正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串
  • 在很多文本编辑器中,正则表达式通常被用来检索、替换符合某个模式的文本
  • 许多程序设计语言都支持利用正则表达式进行字符串操作

1.2 常用的的正则表达式符号

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

2.location

2.1 分类

location大致可以分为以下三类:

  1. 精准匹配:location = / {…}
  2. 一般匹配:location / {…}
  3. 正则匹配:location ~ / {…}

2.2 常用匹配规则

符号 描述
= 进行普通字符精准匹配,也就是完全匹配
^~ 表示普通字符匹配,使用前缀匹配;如果匹配成功,则不再匹配后续location
~ 区分大小写的匹配
~* 不区分大小写的匹配
!~ 区分大小写的匹配取非
!~* 不区分大小写的匹配取非

2.3 优先级

  1. 首先精准匹配 =
  2. 其次前缀匹配 ^~
  3. 其次是按文件中顺序的正则匹配 ~ 或 ~*
  4. 然后是匹配不带任何修饰的前缀匹配,即一般匹配
  5. 最后交给 / 通用匹配,优先级最低

2.4 示例

  1. localtion = / {}
    =为精准匹配 /,主机名后面不能带任何字符串,比如访问 / 和 /xcf,则 / 匹配,/xcf 不匹配
  1. location / {}
    因为所有的地址都以 / 开头,所以这条规则将匹配到所有的请求,比如访问 / 和 /data,则 / 匹配,/data也匹配
    但若后面是正则表达式,则会和最长字符串优先匹配(最长匹配)
  1. location /documents/ {}
    匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它 location
  1. location /documents/abc {}
    匹配任何以 /documents/abc 开头的地址,匹配符合后,还要继续往下搜索其他 location
    只有其他 location 后面的正则表达式没有匹配到时,才会采用这一条
  1. location ^~ /images/ {}
    匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,并采用这条
  1. location ~* .(gif|jpg|jpeg)$ {}
    匹配所有以 gif、jpg、jpeg 为结尾的请求
    然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则
  1. location /images/abc {}
    最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location,会发现 ^~ 和 ~ 存在
  1. location ~ /images/abc {}
    匹配以 /images/abc 开头的,优先级次之,只有去掉 location ^~ /images 才会采用这一条
  1. location /images/abc/1.html {}
    匹配 /images/abc/1.html 文件,如果和正则 ~ /images/abc/1.html 相比,正则优先级更高
  1. 优先级总结:
    (location = 完整路径) > (location ^~ 路径) > (location ,* 正则顺序) > (location 部分起始路径) > (location /)

2.5 匹配规则定义

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

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

location / {
    
    
    root
    html;
    index
    index.html index.html;
}

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

location ^~ /static/ {
    
    
    root /webroot/static/;
}

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

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

location / {
    
    
    proxy_ pass http://tomcat_server;

二、rewrite

1.概述

  • rewrite 功能就是:使用 nginx 提供的全局变量或自己设置的变量,结合正则表达式和标记位实现 URL(我们访问的完整域名+路径) 重写以及重定向
  • 比如:更换域名后需要保持旧的域名能跳转到新的城名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求
  • rewrite 只能放在 server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用
  • 例如:http://www.xcf.com/a/w/index.php?id=1&u=str 只对/a/we/index.php重写

2.跳转实现

mark

  • 我们之前学习的防盗链其实就是基于 rewrite 重写的功能来实现
  • Nginx 使用更多,且较于 Apache 效率更快
  • Nginx:通过 ngx_http_rewrite_module 模块支持 URL 重写、支持 if 条件判断,但不支持 els
  • 跳转:从一个 location 跳转到另一个 location,循环最多可以执行 10 次,吵过后 nginx 将返回 500错误
  • PCRE 支持:perl 兼容正则表达式的语法规则匹配
  • 重写模块 set 指令:创建新的变量并设其值

3.跳转场景

  • 调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求
  • 为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态URL地址伪装成静态地址提供服务
  • 网址换新域名后,让旧的访问跳转到新的域名上;例如,访问京东的 360buy.com 会跳转到 jd.com
  • 服务端某些业务调整,比如根据特殊变量、目录、客户端的信息进行URL调整等

3.执行顺序

  1. 执行 server 块里面的 rewrite 指令
  2. 执行 location 匹配
  3. 执行选定的 location 中的 rewrite 指令

4.语法与flg标记说明

rewrite <regex> <replacement> [flag];
  • regex:表示正则匹配规则
  • replacement:表示跳转后的内容
  • flag:表示 rewrite 支持的 flag 标记

flag标记说明:

  1. last :本条规则匹配完成后,继续向下匹配新的location URL规则,一般用在server和if中。
  2. break:本条规则匹配完成即终止,不再匹配后面的任何规则,一般使用在location 中
  3. redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址
  4. permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

三、rewrite示例

环境准备:

  • 安装本地YUM源仓库
  • 源码编译安装Nginx服务
  • VMware,基于CentOS7
  • 一台源主机CentOS7(192.168.126.15)
  • 一台Win10(192.168.126.10)用作测试
  • 以上有疑问的可以翻看我以前的博客

1.基于域名的跳转

需求:现在公司旧域名www.xcf.com有业务需求变更,需要使用新域名www.zxc.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变

扫描二维码关注公众号,回复: 12155860 查看本文章
mkdir -p /var/log/nginx/

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.xcf.com;
                        #修改域名       

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;
                ##日志保存路径修改

        location / {
    
    
        if ($host = 'www.xcf.com') {
    
    
        #$host为rewrite全局变量,代表请求主机头字段或主机名
                rewrite ^/(.*)$ http://www.zxc.com/$1 permanent;
                                #$1为正则匹配得内容,即域名后边得字符串
        }
            root   html;
            index  index.html index.htm;
        }


echo "192.168.126.15 www.xcf.com" >> /etc/hosts
echo "192.168.126.15 www.zxc.com" >> /etc/hosts

systemctl restart nginx.service 

mark

此时打开浏览器,访问 http://www.xcf.com 时会自动跳转至 zxc
mark

2.基于客户端IP访问跳转

需求:公司业务新版本上线,要求所有 IP 访问任何内容都显示是一个固定维护的页面,只有公司 IP 192.168.126.15 访问正常

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.xcf.com;

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;
        ##设置是否是合法的IP标记
        set $rewrite true;
        #设置变量$rewrite,变量值为boole值true

        #判断是否为合法IP
        if ($remote_addr = "192.168.126.15") {
    
    
                set $rewrite false;
                #当客户端IP为192.168.126.15时,将变量值设为flase,不进行重写
        }

        ##除了合法IP,其它都是非法IP,进行重写跳转到维护页面
        if ($rewrite = true) {
    
    
        #当变量值为true时,进行重写
                rewrite (.+) /weihu.html;
                #重写在访问IP后边插入/weihu.html,例如192.168.126.65/weihu.html
        }
        location = /weihu.html {
    
    
            root /var/www/html;
        #页面返回/var/www/html/weihu.html的内容
        }

        location / {
    
    
            root   html;
            index  index.html index.htm;


mkdir -p /var/www/html

echo '<h1>weihu!!</h1>' > /var/www/html/weihu.html

systemctl restart nginx.service 

mark

进入win10,修改配置文件,用以映射源主机DNS域名解析
mark

访问试试
mark
只有IP 为 192.168.126.15 的源主机才可以进行正常访问网站

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

需求:网站(http://bbs.xcf.com)更换域名,需要将旧域名下的访问都跳转至新网址中(http:www.xcf.com/bbs)

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  bbs.xcf.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;
        #添加
        location /post {
    
    
                rewrite (.+) http://www.xcf.com/bbs$1 permanent;
                #这里$1为位置变量,代表/post
        }
        location / {
    
    
            root   html;
            index  index.html index.htm;
        }



mkdir -p /usr/local/nginx/html/bbs/post

echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.126.15 bbs.xcf.com" >> /etc/hosts

systemctl restart nginx.service 

mark

此时用浏览器访问 http://bbs.xcf.com/post/1.html 会自动跳转到 http://www.xcf.com/bbs/post/1.html
mark

4.基于参数匹配的跳转

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

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.xcf.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;

        if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$) {
    
    
        #设置正则匹配
                rewrite (.*) http://www.xcf.com permanent;
                #设置重写
        }
        location / {
    
    
            root   html;
            index  index.html index.htm;
        }



systemctl restart nginx.service 

使用浏览器访问 http://www.xcf.com/100-100-100.html 或 http://www.xcf.com/100-200-100.html会自动跳转到 http://www.xcf.com页面
mark

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

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

    server {
    
    
        listen       80;
        server_name  www.xcf.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;

        location ~* /upload/.*\.php$ {
    
    
           rewrite (.+) http://www.xcf.com permanent;
        }

        location / {
    
    
            root   html;
            index  index.html index.htm;
        }


systemctl restart nginx.service

浏览器访问 http://www.xcf.com/upload/123.php 跳转到 http://www.xcf.com 首页mark

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

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

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.xcf.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;

        location ~* /abc/123.html {
    
    
           rewrite (.+) http://www.xcf.com permanent;
        }

        location / {
    
    
            root   html;
            index  index.html index.htm;
        }


systemctl restart nginx.service 

浏览器访问 http://www.xcf.com/abc/123.html 跳转到 http://www.gcc.com
mark

猜你喜欢

转载自blog.csdn.net/weixin_51486343/article/details/112644626