location应用

location应用

location区段,通过指定模式来与客户端请求的URI相匹配

//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
//语法:location [ 修饰符 ] pattern {…}

常用修饰符说明

修饰符功能
=精确匹配
~正则表达式,区分大小写
~*正则表达式,不区分大小写
^~前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等

没有修饰符

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location /qwe {
            echo "qwe";
        }
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl 192.168.147.66/qwe
qwe

//修改配置文件和上面的一样,这里就不一一书写了
[root@localhost ~]# curl 192.168.147.66/qwe\?p1\=11\&p2\=22
qwe
[root@localhost ~]# curl 192.168.147.66/qwe/
qwe
[root@localhost ~]# curl 192.168.147.66/qwert
qwe

=:表示必须与指定的模式精确匹配

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

 location /qwe {
            echo  "qwe";
        }
        location /qwert {
            echo  "qwert";
        }
[root@localhost ~]# nginx -s reload

[root@localhost ~]# curl 192.168.147.66/qwe
qwe
[root@localhost ~]# curl 192.168.147.66/qwert
qwert
[root@localhost ~]# curl 192.168.147.66/qwi
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost ~]# curl 192.168.147.66/qwrty
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

[root@localhost ~]# curl 192.168.147.66/qwertyu
qwert
[root@localhost ~]# curl 192.168.147.66/qwe\?p1\=11\&p2\=22
qwe

~:表示指定的正则表达式要区分大小写

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location ~ ^/qwert$ {
            echo  "qwert";
        }

[root@localhost ~]# curl 192.168.147.66/qwert
qwert
[root@localhost ~]# curl 192.168.147.66/qwerT
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

~*:表示指定的正则表达式不区分大小写

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location ~* ^/qwert$ {
            echo  "qwert";
        }

[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl 192.168.147.66/qwert
qwert
[root@localhost ~]# curl 192.168.147.66/qwerT
qwert
[root@localhost ~]# curl 192.168.147.66/QWERT
qwert

查找顺序和优先级:

由高到底依次为

  1. 带有=的精确匹配优先
  2. 正则表达式按照他们在配置文件中定义的顺序
  3. 带有^~修饰符的,开头匹配
  4. 带有~ 或 ~*修饰符的,如果正则表达式与URI匹配
  5. 没有修饰符的精确匹配
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

        location = /qwe {
            echo  "qwe";
        }

        location ~ /qwert {
            echo  "qwert";
        }

        location ~* ^/qwert$ {
            echo  "aabb";
        }

[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl 192.168.147.66/qwert
qwert
[root@localhost ~]# curl 192.168.147.66/qwe
qwe
[root@localhost ~]# curl 192.168.147.66/qwer
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

没有精准匹配符

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
       # location = /qwe {
        #    echo  "qwe";
       # }

        location ~ ^/qwert {
            echo  "qwert";
        }

        location ~* ^/qwert$ {
            echo  "aabb";
        }


[root@localhost ~]# curl 192.168.147.66/qwert
qwert
[root@localhost ~]# curl 192.168.147.66/qwer
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

[root@localhost ~]# curl 192.168.147.66/qwerty
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

优先级次序如下

( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~ 正则 ) --> ( location 路径 )*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值