Nginx的location匹配和重定向rewrite

Nginx的location匹配和重定向rewrite

一、location匹配

从功能看 rewrite 和 location 似乎有点像,都能实现跳转,主要区别在于 rewrite 是在同一域名内更改获取资源的路径,而 location 是对一类路径做控制访问或反向代理,还可以proxy_pass 到其他机器。

1.1 location匹配分类(三类)

精准匹配:location = / {……}

一般匹配:location / {……}

正则匹配:location ~ / {……}

1.2 location常用的匹配规则

=:进行普通字符精确匹配,也就是完全匹配。

^~:表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其它location。

~:区分大小写的匹配。

~*:不区分大小写的匹配。

!~:区分大小写的匹配取非。

!~*:不区分大小写的匹配取非。

补:常用的nginx正则表达式
^ :匹配输入字符串的起始位置
$ :匹配输入字符串的结束位置
* :匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”
+ :匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o”
? :匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}. :匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式
\ :将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$”
\d :匹配纯数字
{n} :重复 n 次
{n,} :重复 n 次或更多次
{n,m} :重复 n 到 m 次
[] :定义匹配的字符范围
[c] :匹配单个字符 c
[a-z] :匹配 a-z 小写字母的任意一个
[a-zA-Z0-9] :匹配所有大小写字母或数字
() :表达式的开始和结束位置
| :或运算符    
1.3 location优先级
  1. 首先精确匹配 =
  2. 其次前缀匹配 ^~
  3. 其次是按文件中顺序的正则匹配 *
  4. 然后匹配不带任何修饰的前缀匹配
  5. 最后是 / 通用匹配
1.4 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 {}     www.baidu.com/
匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条

(5)location ^~ /images/ {}
匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条

(6)location ~* \.(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 结尾的请求
然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则

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

优先级总结:

(location = 完整路径) > (location ^~ 路径) > (location ,* 正则顺序) > (location 部分起始路径) > (location /)

1.5 总结location使用

实际网站使用中,需要有至少三个匹配规则定义,第一必选规则,第二必选规则和第三通用规则。

#第一个必选规则
直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网。
可以是一个静态首页,也可以直接转发给后端应用服务器
location = / {
root html;
index index.html index.htm;
}

#第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}

/static/

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

#第三个规则就是通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器
非静态文件请求就默认是动态请求
location / {
proxy_pass http://tomcat_server;
}

二、重定向rewrite

rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。
比如:更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。

注:rewrite只能放在server{},location{},if(){}中,并且默认只对域名后边的除去传递的参数外的字符串起作用。

例如:
    http://www.setting.com/abc/date/index.php
    rewrite只对/abc/date/index.php重写
2.1 rewrite跳转实现
  • Nginx:通过ngx_http_rewrite_module 模块支持URL重写、支持if条件判断,但不支持else
  • 跳转:从一个 location跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误
  • PCRE支持:perl兼容正则表达式的语法规则匹配
  • 重写模块 set 指令:创建新的变量并设其值
2.2 rewrite执行顺序
  • 执行server块里面的rewrite指令
  • 执行location匹配
  • 执行选定的location中的rewrite指令
2.3 语法格式
rewrite <regex> <replacement> [flag];

regex :表示正则匹配规则。
    
replacement :表示跳转后的内容。
    
flag :表示 rewrite 支持的 flag 标记。
flag标记作用
last停止所有rewrite相关指令,然后使用新的URI进行location匹配。
break停止所有rewrite相关指令,和last不同的是,last接着继续是u哦那个新的URI匹配location,而break则是直接使用当前的URI进行请求处理,能避免重复rewrite,last一般在server,break一般在location。
redirect返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent返回301永久重定向,浏览器 地址栏会显示跳转后的URL地址

三、location匹配和rewrite使用

1、基于域名的跳转

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

    server {
        listen      192.168.10.10:80;
        server_name  www.setting.com;

        charset uft-8;

        #access_log  logs/host.access.log  main;

        location / {
                if ($host = 'www.setting.com') {
                        rewrite ^/(.*)$ http://www.test.com/$1 permanent;
                }
            root   html;
            index  index.html index.htm;

        }

image-20220725163114310

image-20220725163324452

image-20220725164335424

image-20220725162830076

浏览器输入模拟访问 http://www.setting.com/test/1.html
会跳转到www.test.com/test/1.html,查看元素可以看到返回301,实现了永久重定向跳转,而且域名后的参数也正常跳转。

rewrite ^/(.*)$ http://www.test.com/$1 permanent;
添加/则可以帮助重定向,访问域名后的具体主页。

image-20220725174149683

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

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

    server {
        listen      192.168.10.10:80;
        server_name  www.setting.com;

        charset utf-8;

        set $rewrite true;
        if ( $remote_addr = "192.168.10.10") {
                set $rewrite false;
        }
        if ( $rewrite = true) {
                rewrite (.+) /weihu.html;
        }
        location = /weihu.html {
                root /var/www/html;
        }
        #access_log  logs/host.access.log  main;

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

image-20220725181321344

image-20220725180518948

image-20220725180827729

3、基于旧域名跳转到新域名后面家目录

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

    server {
        listen      192.168.10.10:80;
        server_name  www.setting.com;

        charset utf-8;

        location = /weihu.html {
                root /var/www/html;
        }
        #access_log  logs/host.access.log  main;

        location /test {
                rewrite (.+) http://www.test.com/test$1 permanent;
        }

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

        }

image-20220725192942527

image-20220725193047114

image-20220725192752670

4、基于参数匹配的跳转

现在访问http://www.setting.com/100-(100|200)-100.html 跳转到http://www.test.com页面。

  server {
        listen      192.168.10.10:80;
        server_name  www.setting.com;

        charset utf-8;

        location = /weihu.html {
                root /var/www/html;
        }
        #access_log  logs/host.access.log  main;

        if ($request_uri ~ ^/1-(2|3)-(\d+).html$) {
                rewrite (.+) http://www.test.com permanent;
        }
        location / {
            root   html;
            index  index.html index.htm;

        }

image-20220725194340054

image-20220725194308981

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

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

 server {
        listen      192.168.10.10:80;
        server_name  www.setting.com;

        charset utf-8;

        location = /weihu.html {
                root /var/www/html;
        }
        #access_log  logs/host.access.log  main;
        location ~* /upload/.*\.php$ {
                rewrite (.+) http://www.setting.com permanent;
        }
        location / {
            root   html;
            index  index.html index.htm;

        }

image-20220725194819843

image-20220725194714699

6、将任意 url 请求的跳转

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

 server {
        listen      192.168.10.10:80;
        server_name  www.setting.com;

        charset utf-8;

        location = /weihu.html {
                root /var/www/html;
        }
        #access_log  logs/host.access.log  main;

        location ~* ^/test/test.html {
                rewrite (.+) http://www.setting.com permanent;
        }

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

        }

image-20220725195238028

            root /var/www/html;
    }
    #access_log  logs/host.access.log  main;

    location ~* ^/test/test.html {
            rewrite (.+) http://www.setting.com permanent;
    }

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

    }

image-20220725195206383

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值