温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Rewrite跳转

发布时间:2020-06-29 22:57:59 来源:网络 阅读:89550 作者:友引町 栏目:系统运维

Nginx服务中Rewrite的应用

Rewrite跳转场景

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

Rewrite跳转实现

Rewrite跳转

Rewrite实用场景

Nginx跳转需求的实现方式

  • 使用rewrite进行匹配跳转
  • 使用if匹配全局变量后跳转
  • 使用location匹配再跳转

rewrite放在server{},if{},location{} 段中

对域名或参数字符串

  • 使用if全局变量匹配
  • 使用proxy_ pass反向代理

Nginx正则表达式

常用的正则表达式元字符

字符 说明
^ 匹配输入字符串的起始位置
$ 匹配输入字符串的结束位置
* 匹配前面的字符零次或多次
+ 匹配前面的字符一次或多次
? 匹配前面的字符零次或一次
. 匹配除“\n”之外的任何单个字符。使用诸如"[.\n]"之 类的模式,可匹配包括“n”在内的任意字符
\ 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用
\d 匹配纯数字
{n} 重复n次
{n,} 重复n次或更多次
[c] 匹配单个字符c
[a-z] 匹配a-z小写字母的任意一个
[a-zA-Z] 匹配a-z小写字母或A-Z大写字母的任意一 个

Rewrite命令

语法

rewrite    <regex>   <replacement>   [flag];
<regex>  //正则     <replacement>   //跳转后的内容    [flag]   //rewrite支持flag标记

flag标记说明

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

last和break比较

last break
使用场景 一般写在serverif 一般使用在location
URL匹配 不终止重写后的url匹配 终止重写后的url匹配

location分类

分类

location = patt {}       [精准匹配]
location patt {}         [一般匹配]
location ~ patt {}       [正则匹配]

正则匹配的常用表达式

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

location优先级

相同类型的表达式,字符串长的会优先匹配

按优先级排列

  • =类型
  • ^~类型表达式
  • 正则表达式(~~*)类型
  • 常规字符串匹配类型,按前缀匹配
  • 通用匹配(/),如果没有其它匹配,任何请求都会匹配到

比较rewrite和location

相同点

  • 都能实现跳转

不同点

  • rewrite是在同一域名内更改获取资源的路径
  • location是对一-类路径做控制访问或反向代理,还可以proxy_pass到其他机器

rewrite会写在location里, 执行顺序

  • 执行server块里面的rewrite指令
  • 执行location匹配
  • 执行选定的location中的rewrite指令

Location优先级的示例

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

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

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

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

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

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

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

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

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

location优先级规则

匹配某个具体文件

  • (location =完整路径) > (location ^~完整路径) > (location ~*完整路径) > (location ~完整路径) > (location 完整路径) > (location /)

用目录做匹配访问某个文件

  • (location= 目录) > (location ^~目录/) > (location ~ 目录) > (location ~*目录) > (location 目录) > (location /)

配置实例

首先安装Nginx服务,安装并配置DNS服务

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm     //安装nginx官方源码包
获取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.ymMjSa: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost ~]# yum install nginx -y     //安装nginx服务
已加载插件:fastestmirror, langpacks
已加载插件:fastestmirror, langpacks
base                                                                       | 3.6 kB  00:00:00     
extras                                                                     | 2.9 kB  00:00:00     
...//省略部分内容...
已安装:
  nginx.x86_64 1:1.16.1-1.el7.ngx                                                                   
完毕!
[root@localhost ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf                 //配置文件路径
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf     //编辑配置文件
server {
    listen       80;
    server_name  www.aacp.com;           //更改域名

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;   //更改日志文件名称,并开启功能

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
...//省略部分内容...
:wq
[root@localhost ~]# yum install bind -y          //安装DNS功能
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
...//省略部分内容...
已安装:
  bind.x86_64 32:9.11.4-9.P2.el7 
  ...//省略部分内容...
完毕!
[root@localhost ~]# vim /etc/named.conf
...//省略部分内容...
options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };
...//省略部分内容...
:wq
[root@localhost ~]# vim /etc/named.rfc1912.zones
...//省略部分内容...
zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};
...//省略部分内容...
:wq
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.144.133
:wq
[root@localhost named]# systemctl start named               //启动DNS服务
[root@localhost named]# systemctl start nginx               //启动nginx服务
[root@localhost named]# systemctl stop firewalld.service    //关闭防火墙
[root@localhost named]# setenforce 0                        //关闭增强性安全功能
[root@localhost named]# netstat -ntap | grep nginx       //查看nginx服务是否开启
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2769/nginx: master 

在客户机中测试nginx服务是否成功搭建

Rewrite跳转Rewrite跳转

配置基于域名的跳转

公司旧域名www.accp.com, 因业务需求有变更,需要使用新域名www.kgc.com代替

  • 不能废除旧域名
  • 从旧域名跳转到新域名,且保持其参数不变

配置实例

[root@localhost named]# vim /etc/nginx/conf.d/default.conf     //编辑Nginx配置文件
...//省略部分内容...
 #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;

    location / {
        if ( $host = "www.accp.com" ){   
               rewrite ^/(.*)$ http://www.kgc.com/$1 permanent;
           //在location模块中添加if判断语句,当输入"www.accp.com"访问网页时跳转到www.kgc.com中
         }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }   

    #error_page  404              /404.html;
...//省略部分内容...
:wq
[root@localhost named]# vim /etc/named.rfc1912.zones
...//省略部分内容...
zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};

zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};      
...//省略部分内容...
:wq
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
[root@localhost named]# systemctl restart nginx
[root@localhost named]# systemctl restart named

在客户机中测试访问

Rewrite跳转Rewrite跳转Rewrite跳转Rewrite跳转

配置基于客户端IP访问跳转

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

配置实例

  • 开启两台客户机,分别查看两台客户端IP地址,并在服务器配置可以访问的IP地址

Rewrite跳转Rewrite跳转

[root@localhost conf.d]#  vim default.conf        //编辑配置文件
...//省略部分内容...
#charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;

    set $rewrite true;              //设置是否合法的IP标记
    if ($remote_addr = "192.168.144.128"){    //判断是否为合法地址,如果为合法地址执行操作
            set $rewrite false;
    }
    if ($rewrite = true){         //判断是否为非法地址,如果为非法地址则打上标记,合法地址不做操作
            rewrite (.+) /main.html;
    }
    location = /main.html {         //匹配标记,执行跳转
            root /usr/share/nginx/html;
    }

    location / {     //注意此处删除上面设置的基于域名跳转的配置条目
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
...//省略部分内容...
:wq
[root@localhost conf.d]# cd /usr/share/nginx/html/      //进nginx服务站点
[root@localhost html]# ls         //查看信息
50x.html  index.html
[root@localhost html]# vim main.html      //编辑跳转的网页内容
<h2>this is test web</h2>
:wq
[root@localhost html]# systemctl restart nginx    //重启网站

分别在客户机中访问测试

Rewrite跳转Rewrite跳转

配置基于旧、新域名跳转并加目录

  • 将域名http://bbs.accp.com下面的发帖都跳转到http://www.accp.com/bbs,且域名跳转后保持参数不变
[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  bbs.aacp.com;          //更改域名

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location /post {               //设置匹配字段,字段匹配执行跳转操作
          rewrite (.+) http://www.accp.com/bbs$1 permanent;
    }
                             //注意删除上面设置的基于客户端IP访问的跳转的配置条目
    location / {     
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
...//省略部分内容...
:wq
[root@localhost html]# vim /var/named/accp.com.zone        //编辑DNS服务区域数据文件
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
bbs IN  A       192.168.144.133          //更改主机头解析
:wq
[root@localhost html]# systemctl restart nginx      //重启nginx服务
[root@localhost html]# systemctl restart named       //重启DNS服务
  • 在客户机中测试访问

Rewrite跳转Rewrite跳转

配置基于匹配参数访问的跳转

  • 浏览器访问http://www.accp.com/100-(100|200)-100.html跳转到http://www.accp.com页面
[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.aacp.com;       //将服务器域名更改会www

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$){    //删除此处上面设置的基于新、旧域名跳转并加目录跳转的配置条目
          rewrite (.*) http://www.accp.com permanent;     并设置基于参数访问时跳转回主网页
    }

    location / {     
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
...//省略部分内容...
:wq
[root@localhost html]# vim /var/named/accp.com.zone
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.144.133      //更改主机头解析
:wq
[root@localhost html]# systemctl restart nginx        //重启nginx服务
[root@localhost html]# systemctl restart named        //重启DNS服务
  • 在客户机中访问测试

Rewrite跳转Rewrite跳转

配置基于目录下所有php文件跳转

  • 访问http://www.accp.com/upload/1.php跳转到首页
[root@localhost html]# vim /etc/nginx/conf.d/default.conf     //编辑配置文件
server {
    listen       80;
    server_name  www.aacp.com;

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location ~* /upload/.*\.php$ {        //删除上面设置的匹配参数跳转访问,配置匹配所有php结尾访问跳转回主页
          rewrite (.+) http://www.accp.com permanent;
    } 

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;
...//省略部分内容...
:wq
[root@localhost html]# systemctl restart nginx        //重启nginx服务
  • 在客户机中访问测试

Rewrite跳转Rewrite跳转

配置基于最普通url请求的跳转

  • 访问一个具体的页面跳转到首页
[root@localhost html]# vim /etc/nginx/conf.d/default.conf    //编辑配置文件
server {
    listen       80;
    server_name  www.aacp.com;

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location ~* ^/abc/123.html {        //删除上面的配置,并重新编辑以具体的某个页面访问网页时跳转回主页
          rewrite (.+) http://www.accp.com permanent;
    }

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;
...//省略部分内容...
:wq
[root@localhost html]# systemctl restart nginx        //重启nginx服务
  • 在测试机中测试访问

Rewrite跳转Rewrite跳转

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI