关于nginx的location匹配

nginx中location的语法格式是:
location [=|~|~*|^~|@]  /uri/  {
     ......
}

说明:
=    表示完全一致
~    表示区分大小写的正则匹配,
~*   表示不区分大小写的正则匹配
^~  表示不要再继续匹配正则
@    表示内部重定向,不需要跟浏览器交互,是服务端的一个转发

location中的几个概念:
普通location:location using literal strings
正则location:location using regular expressions,有~或~*做前缀的
精确location:匹配到文件名的,如 /first/second/file.html
最大前缀匹配:这个概念在普通location进行匹配的时候会用到,如,访问url为/first/second/index.html时,有/first/和/first/second/两个location存在的话,/first/second/就是此url的最大前缀匹配

location中的匹配原则:
先匹配普通location,再匹配正则location;正则location被匹配到了,会覆盖普通location
一般的匹配过程如下:
a) 普通location的匹配跟配置文件中记录的顺序是无关的
b) 正则location的匹配跟配置文件中记录的顺序有关,先匹配到后就不再向下匹配
c) 普通location里面的匹配是按照最大前缀匹配的原则匹配
d) 普通location的最大前缀匹配到之后,再进行正则匹配
e) 如果被正则location匹配到,会覆盖普通location的最大前缀匹配,精确location也会被正则location覆盖
f) 如果都没有匹配的,则会与location /进行匹配

也有特殊情况,使普通location匹配到后就不再继续正则匹配:
有=或^~前缀的普通location匹配到后就不再进行正则匹配

示例

nginx的页面文件存放目录/usr/share/nginx/html的文件结构如下:
.
├── 50x.html
├── else
│   └── index.html
├── first
│   ├── index.html
│   ├── second
│   │   ├── exactmatch.html
│   │   ├── index.html
│   │   ├── test.htm
│   │   ├── test.html
│   │   └── third
│   │       ├── exactmatch.htm
│   │       ├── exactmatch.html
│   │       ├── index.html
│   │       ├── test1.html
│   │       ├── test2.html
│   │       ├── test.htm
│   │       └── test.html
│   ├── test.htm
│   └── test.html
├── index.html
├── test.htm
└── test.html

注:文件内容为文件的路径,如first目录下的index.html里面的内容为:/first/index.html

nginx配置内容如下:
     server {
        listen       80;
        server_name  192.168.0.125;
        server_name  localhost;
        root html;

        location / {
            index  index.html;
            deny   all;
        }

        location  /first/ {
            index  index.html;
        }

        location  /first/second/ {
            index  test.htm;
        }

        location /first/second/exactmatch.html {
            index  index.html;
        }

        location ^~ /first/second/third/ {
            index  index.html;
        }

        location /first/second/third/exactmatch.html {
            index  index.html;
        }

        location /first/second/third/exactmatch.htm {
            index  index.html;
        }

        location = /first/second/third/test1.html {
            index  index.html;
        }

        location /first/second/third/test2.html {
            index  index.html;
        }

        location ~ \.html$ {
            rewrite .* /else/index.html break;
        }

        error_page  404 = @notfound;

        location @notfound {
            proxy_pass http://www.taobao.com;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

测试练习(此次nginx用的server_name为192.168.0.125)
访问url
访问结果
说明
http://192.168.0.125/first/
/else/index.html
默认的index.html是.html结尾的,被正则匹配
http://192.168.0.125/first/test.htm
/first/test.htm
普通location匹配
http://192.168.0.125/first/test.html
/else/index.html
正则location匹配
http://192.168.0.125/first/second/
/first/second/test.htm
index指定的是test.htm,不是以.html结尾了,所以不匹配正则
http://192.168.0.125/first/second/exactmatch.html
/else/index.html
精确匹配,但是被正则覆盖了
http://192.168.0.125/first/second/third/
/first/second/third/index.html
^~前缀的原因,普通location匹配之后,不再匹配正则
http://192.168.0.125/first/second/third/exactmatch.html
/else/index.html
精确匹配,但是被正则覆盖了
http://192.168.0.125/first/second/third/exactmatch.htm
/first/second/third/exactmatch.htm
精确匹配
http://192.168.0.125/first/second/third/test1.html
/first/second/third/test1.html
=前缀的原因,不匹配正则
http://192.168.0.125/first/second/third/test2.html
/else/index.html
没有=前缀,精确匹配后被正则覆盖了
http://192.168.0.125/first/second/third/notfound.html
404转到淘宝页面
notfound.html页面是不存在的,虽然以.html结尾,但是最大前缀匹配的是/first/second/third/,它前面是带^~前缀的,所以不匹配正则,就会报404;然而,404又被 @notfound定向到淘宝页面
http://192.168.0.125/first/second/third/notfound.htm
404转到淘宝页面
同上
http://192.168.0.125/first/second/notfound.html
/else/index.html
虽然页面不存在,但是以.html结尾,被正则location匹配
http://192.168.0.125/first/second/notfound.htm
404转到淘宝页面
404且不匹配正则,被@notfound定向到淘宝页面
http://192.168.0.125/first/notfound.html
/else/index.html
虽然页面不存在,但是以.html结尾,被正则location匹配
http://192.168.0.125/first/notfound.htm
404转到淘宝页面
404且不匹配正则,被@notfound定向到淘宝页面
http://192.168.0.125/notfound.html
/else/index.html
虽然页面不存在,但是以.html结尾,被正则location匹配
http://192.168.0.125/notfound.htm
403 Forbidden
页面不存在,也不以.html结尾,按照普通location的原则,与/匹配;然而location /里面定义的deny all将访问Forbidden掉了,于是报了403
http://192.168.0.125/
403 Forbidden
与location /匹配,报403



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值