Nginx Rewrit

Nginx Rewrit概述

1.Rewrit跳转场景

rewrit跳转场景主要包括以下场景

  • 可以调整用户浏览URL,看起来更加规范,合乎开发及产品人员的需求
  • 为了让搜索赢球搜录网站内容及用户体验更好,企业会将动态URL地址伪装成静态地址提供服务
  • 网址换新域名后,让旧的访问跳转到新的域名上。
  • 根据特殊变量、目录、客户端的信息进行URL调整等。
2.Rewrit跳转实现

​ Nginx 是通过 ngx_http_rewrite_module 模块支持 url 重写、支持 if 条件判断,但不支持 else。另外该模块需要 PCRE 支持,应在编译 Nginx 时指定 PCRE 支持,默认已经安装。

3.Rewrit实际应用场景

​ 在实际工作的应用中,Nginx 跳转需求有三种方式可实现。可以直接用 rewrite 进行匹配跳转,也可以使用 if 匹配全局变量后跳转。另外,还可以使用 location 匹配再跳转。

4.Nginx正则表达式

Nginx常用正则表达式如下:

字符描述
^匹配输入字符串的起始位置
$匹配输入字符串的结束位置
*匹配前面的字符零次或多次
+匹配前面的字符一次或多次
?匹配前面的字符零次或一次
.匹配除“\n”之外的任何按个字符
\将后面接着的字符标记为一个特殊字符或一个原义字符
\d匹配纯数字
{n}重复n次
{n,}重复n次或更多次
[c]匹配单个字符
[a-z]匹配a-z小写

Nginx Rewrit基本操作

Rewrite语法

​ Rewrit命令语法如下所示,其中regex表示正则匹配规则、replacement表示跳转后的内容、flag表示rewrite支持的flag标记。

rewrit<regex><replacement>[flag];
flag标记说明
  • last:相当于Apache的[L]标记,表示完成rewrite。常用于server与if中
  • break:本条规则匹配完成即终止,不在匹配后面的任何规则。常用于location中
  • redirect:返回302临时重定向,浏览器地址会像是跳转后的URL地址,爬虫不会更新url(因为是临时)。302为默认flag标记
  • permanent:返回301永久重定向,浏览器地址会显示跳转后的URL地址,爬虫会更新URL
Location分类

location大致可以分为三类,语法如下

location = patt {} [精准匹配]
location patt {}   [一般匹配]
location ~ patt{}  [正则匹配]
常用匹配表达式如下:
正则匹配表达式描述
~表示执行一个正则表达式,区分大小写
~*表示执行一个正则表达式,不区分大小写
!~表示执行一个正则匹配。区分大小写(不匹配)
!~*表示执行一个正则匹配,不区分大小写(不匹配)
^~如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。
=进行普通字符精确匹配,也就是完全匹配
Location优先级

​ 在 Nginx 的 location 配置中 location 的顺序没有太大关系。匹配优先级和 location 表达式的类型有关:相同类型的表达式,字符串长的会优先匹配。

以下是按优先级排列说明:
  • 等号类型(=)的优先级最高,一旦匹配成功,则不再查找其他匹配项。
  • ^~类型表达式。一旦匹配成功,则不再查找其他匹配项
  • 正则表达式类型(*)的优先级次之。
  • 常规字符串匹配类型。按前缀匹配。
  • 通用匹配(/),如果没有其它匹配,任何请求都会匹配到。

Rewrit常见示例

  1. 基于域名的跳转

    #设置好客户端和服务器端的hosts文件
    192.168.10.102 www.benet.com 
    192.168.10.103 www.accp.com
    #修改 Nginx 默认站点配置文件
    [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        server {
            listen       80;
            server_name  www.benet.com;
    
            charset utf-8;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    #基于域名的跳转
    [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
            listen       80;
            server_name  www.benet.com;
    
            charset utf-8;
    
            #access_log  logs/host.access.log  main;
    
        location / {
            root   html;
            index  index.html index.htm;
    
            if ($host = 'www.benet.com') 
              {
              rewrite ^/(.*)$ http://www.accp.com/$1 permanent; 
              }
    
        }
    }
    [root@localhost ~]# systemctl restart nginx
    
    
  2. 基于客户端IP访问跳转

    #修改 Nginx 默认站点配置文件
    [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.benet.com;
    
        #access_log  /var/log/nginx/host.access.log  main;
    
    
        set $rewrite true;
        if ($remote_addr = "192.168.10.51") {
            set $rewrite false;
        }
        if ($rewrite = true) {
        rewrite (.+) /weihu.html;
        }
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    [root@localhost ~]# systemctl restart nginx
    
  3. 基于旧域名跳转到新域名后面加目录

    [root@localhost html]# cd /usr/local/nginx/html/
    [root@localhost html]# mkdir -p bbs/post
    [root@localhost html]# cd bbs/post
    [root@localhost post]# echo "i am bbs">index.html
    [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  bbs.benet.com;
        #access_log  /var/log/nginx/host.access.log  main;
        location /post {
            rewrite (.+) http://www.benet.com/bbs$1 permanent;
        }
        location / {
            root   html;
            index  index.html index.htm;
    }
    }
    [root@localhost ~]# systemctl restart nginx
    
  4. 基于参数匹配的跳转

    #基于参数匹配的跳转
    [root@localhost post]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  bbs.benet.com;
        #access_log  /var/log/nginx/host.access.log  main;
        if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
            rewrite (.*) http://www.benet.com permanent;
        }
        location / {
            root   html;
            index  index.html index.htm;
    }
    [root@localhost ~]# systemctl restart nginx
    
  5. 基于目录下以php结尾的文件跳转

    #基于目录下所有 php 结尾的文件跳转
    要求访问 http://www.benet.com/upload/1.php跳转到首页
    [root@localhost post]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  bbs.benet.com;
        #access_log  /var/log/nginx/host.access.log  main;
        location ~* /upload/.*\.php$ {
            rewrite (.+) http://www.benet.com permanent;
        }
        location / {
            root   html;
            index  index.html index.htm;
    
        }
    }
    [root@localhost ~]# systemctl restart nginx
    
  6. 基于最普通一条url请求的跳转

    #基于最普通一条 url 请求的跳转
    [root@localhost post]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  bbs.benet.com;
        #access_log  /var/log/nginx/host.access.log  main;
        location ~* ^/1/test.html {
            rewrite (.+) http://www.benet.com permanent; 
        }   
        location / {
            root   html;
            index  index.html index.htm;
    
        }
    [root@localhost ~]# systemctl restart nginx
    
  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值