• Nginx Rewrite



    一、Rewrite跳转

    主要职能:基于本地服务器路劲的重写
    一般用于本地不同文件的加载或路径的改写

    1.Rewrite跳转场景

    • 调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求
    • 企业会将动态URL地址伪装成静态地址提供服务
    • 网址换新域名后,让旧访问跳转到新的域名上
    • 服务端某些业务调整

    2.Reweite实际场景

    2.1 Nginx跳转需求的实现方式

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

    2.2 Rewrite执行顺序

    • 执行server 模块里的rewrite 指令
    • 执行选定的location 中的rewrite 指令
    • 执行选定的location 中 if 中的rewrite 指令

    3.Rewrite命令

    3.1 Rewrite 命令语法

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

    3.2 Nginx常用正则表达式元字符

    ^ 匹配输入字符串的起始位置
    $ 匹配输入字符串的结束位置
    * 匹配前面的字符零次或多次
    + 匹配前面的字符一次或多次
    ? 匹配前面的字符零次或一次
    . 匹配除“\n”之外的任何单个字符
    \ 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用
    \d 匹配纯数字
    \w 匹配字母或数字或下划线或汉字
    \s 匹配任意的空白符
    \b 匹配单词的开始或结束
    {n} 重复n次
    {n,} 重复n次或多次
    {n,m} 重复n到m次
    {,m} 最多重复m次
    [] 定义匹配的字符范围,[a]:匹配单个字符a
    () 表达式的开始和结束位置
    | 或表达式

    3.3 flag标记说明

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

    二、location

    1.location分类

    location = / {}精确匹配 /
    location / {}一般匹配(因为所有的地址都以 / 开头,所以这条规则将匹配到所有的请求)
    location ~ / {}按照正则表达式的方式匹配 /

    2.location优先级

    • 相同类型的表达式,字符串长度会优先匹配
    • 按优先级排列
      = 类型
      ^~ 类型表达式
      正则表达式(*)类型
      常规字符串匹配类型,按前缀匹配
      通用匹配( / ),如果没有其他匹配,任何请求都会匹配到

    2.1 正则匹配的常用表达式

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

    2.2 location优先级规则

    精确性越高,优先级越大

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

    3 、rewrite与location比较

    3.1 相同点

    • 都能实现跳转

    3.2 不同点

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

    3.3 rewrite会写在location里,执行顺序

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

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

    必选规则

    • 直接匹配网站根 (加速访问网站首页)
      必选规则
    • 处理静态文件请求(目录匹配或后缀匹配)
      通用规则
    • 通用规则(用于转发动态请求到后端服务器)

    5.location示例说明

    1.location = / {}
    =为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配 再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/。

    2.location / {}
    因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求比如访问 / 和 /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/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则(匹配5不匹配6)

    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 相比,正则优先级更高

    Nginx跳转实验

    1、基于域名的跳转

    旧域名www.yyy.com变更,使用www.yzq.com代替,旧域名不废除,跳转的到新域名,且后面参数不变

    未修改前
    在这里插入图片描述

    1.1 添加映射

    [root@yzq nginx]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.28.100 www.yyy.com www.yzq.com
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.2 创建日志目录

    [root@yzq ~]# mkdir -p /var/log/nginx  ##开启日志功能,自定义一个日志存放路径
    
    • 1

    1.3 修改配置文件

    [root@yzq conf]# vim nginx.conf
    [root@yzq conf]# 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

        server {
            listen       80;
            server_name  www.yyy.com;  ##域名修改
    
            #charset koi8-r;
    
            access_log  /var/log/nginx/www.yyy.com-access.log;  ##>开启对日志保存路径进行修改
             location / {     ##原有location位置插入
                 if ($host = `www.yyy.com`){  ##$host(内置变量)为rewrite全局变量,代表请求主机头字段或者主机名
                     rewrite ^/(.*)$ http://www.yzq.com/$1 permanent
    ##$1为       
    }
                 root  html;
                 index index.html index.htm;
              }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    1.4 重启服务,测试

    [root@yzq conf]# systemctl restart nginx
    
    
    • 1
    • 2

    在这里插入图片描述

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

    (上一个实验配置删除)

    2.1 需求

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

    2.2 创建维护页面

    [root@yzq html]# vim weihu.html
    [root@yzq html]# cat weihu.html 
    this is weihu
    
    • 1
    • 2
    • 3

    2.3 修改配置文件

    [root@yzq conf]# vim nginx.conf
    [root@yzq conf]# 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
        server {
            listen       80;
            server_name  www.yyy.com;
    
            #charset koi8-r;
    
            access_log  /var/log/nginx/www.yyy.com-access.log;
            #设置是否合法的IP标记
            set $rewrite true;  #设置变量$rewrite,变量值为布尔值true
              #判断是否为合法IP
              #remode_addr表示客户端
            if ($remote_addr = "192.168.226.100"){  #当客户端IP为192.168.28.100时,将变量值设为false,不进行重写
                set $rewrite false;
            }
            #除了合法IP,其它都是非法IP,进行重写跳转维护页面
            if ($rewrite = true){  #当变量值为true时,进行重写
                rewrite (.+) /weihu.html;  #将域名后边的路径重写成/weihu.html,例如www.yyy.com/weihu.html
            }
            location =/weihu.html {  #网页返回/var/www/html/weihu.html的内容
                    root /var/www/html;
            }
            location / {
                    root  html;
                    index  index.html index.htm;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    重启服务

    [root@yzq html]# systemctl restart nginx
    
    
    • 1
    • 2

    2.4 测试

    其它主机访问
    在这里插入图片描述
    本机访问
    在这里插入图片描述

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

    访问的是 http://bbs.yyy.com/post/1.html,现在需要将这个域名下面的访问都跳转到http://www.yzq.com/bbs/post/1.html

    3.1 创建指定目录

    创建http://bbs.yyy.com/post/1.html路径

    [root@yzq html]# cd /usr/local/nginx
    [root@yzq nginx]# ls
    client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
    conf              html          proxy_temp  scgi_temp
    [root@yzq nginx]# cd html/
    [root@yzq html]# mkdir -p bbs/post
    [root@yzq html]# ls
    50x.html  bbs  error.png  index.html  renwu.jpg
    [root@yzq html]# cd bbs/
    [root@yzq bbs]# ls
    post
    [root@yzq bbs]# cd post/
    [root@yzq post]# vim post
    [root@yzq post]# vim 1.html
    [root@yzq post]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.28.100 bbs.yyy.com www.yzq.com
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.2 修改配置文件

    [root@yzq post]# vim /usr/local/nginx/conf/nginx.conf
    [root@yzq post]# 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    3.3 重启服务,测试

    [root@yzq post]# systemctl restart nginx
    
    
    • 1
    • 2

    访问bbs.yyy.com/post/1.html跳转到www.yzq.com/bbs/post/1.html
    在这里插入图片描述

    4.基于参数匹配的跳转

    4.1 需求

    访问http://www.yyy.com/100-(100|200)-100.html会跳转到https://www.yzq.com

    4.2 修改配置文件

    [root@yzq post]# vim /usr/local/nginx/conf/nginx.conf
    [root@yzq post]# 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    4.3 重启服务,测试

    [root@yzq post]# systemctl restart nginx
    
    
    • 1
    • 2

    在这里插入图片描述

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

    5.1 需求

    要求访问http://www.yyy.com/upload/123.php跳转到www.yzq.com(场景:注册/登录)

    5.2 修改配置文件

    [root@yzq post]# vim /usr/local/nginx/conf/nginx.conf
    [root@yzq post]# 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5.3 重启服务,测试

    [root@yzq post]# systemctl reload nginx.service 
    
    
    • 1
    • 2

    在这里插入图片描述

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

    6.1 需求

    访问 www.yyy.com/asda/123.html 跳转到首页www.yyy.com

    6.2 修改配置文件

    [root@yzq post]# vim /usr/local/nginx/conf/nginx.conf
    [root@yzq post]# 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    6.3 重启服务,测试

    [root@yzq post]# systemctl reload nginx.service 
    
    
    • 1
    • 2

    在这里插入图片描述

    小结(Rewrite的作用)

    调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求,网址换新域名后,让旧访问跳转到新的域名上

  • 相关阅读:
    Linux用户和用户组管理总结
    微擎模块 智慧城市同城 v2.3.46原版打包修复可用,[修复] 手机号登录时,无法正确显示会员积分、余额的问题
    MySQL进阶实战9,InnoDB和MyISAM的数据分布对比
    git 修改远程地址
    强化学习从基础到进阶-案例与实践[4.2]:深度Q网络DQN-Cart pole游戏展示
    JAVA工具类匹配重复或者连续的字符和符号
    后缀数组学习笔记 - 更新ing
    【Linux】Linux任务管理与守护进程
    DIN模型和SIM模型原理与实践
    基于Python-Opencv实现哈哈镜效果
  • 原文地址:https://blog.csdn.net/weixin_71429790/article/details/126610126