nginx上location的扩展!!!

1. location中的相关修饰符!

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

1.1 查找顺序优先级为!!!

  1. 带有=的精确匹配优先
  2. 正则表达式按照他们在配置文件中定义的顺序
  3. 带有^~ 修饰符的,开头匹配
  4. 带有~ 或~*修饰符的,如果正则表达式与URI匹配
  5. 没有修饰符的精确匹
  6. ( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )

2. 测试!!!

2.1 当没有修饰符的时候!!

[root@localhost nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf
        location /abc {
            echo "abc";
        }
[root@localhost nginx-1.20.1]# nginx -s reload
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abc
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcd
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abc\?p1\=1\&p2\=3
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abc/
abc

2.2 精确匹配!!!


[root@localhost nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf
        location  /abc {
            echo "abc";
        }

        location = /abcd {
            echo "abcd";
        }
        
[root@localhost nginx-1.20.1]# nginx -s reload
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcd
abcd
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdfg
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABC
<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>

2.3 区分大小写!!!

[root@localhost nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf
        location  /abc {
            echo "abc";
        }

        location = /abcd {
            echo "abcd";
        }

        location ~ ^/abcdef$ {
            echo "abcdef"}
[root@localhost nginx-1.20.1]# nginx -s reload
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcd
abcd
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdef
abcdef
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdeF
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/AbcdeF
<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 nginx-1.20.1]# curl 192.168.160.150/ABCdeF
<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>

2.4 不区分大小写!!

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

[root@localhost nginx-1.20.1]# nginx -s reload
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdefg
abcdefg
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABCDEFG
abcdefg
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABCdefg
abcdefg
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABC
<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>

2.5 优先级!!!


          location  /abc {
            echo "abc";
        }

        location = /abc {
            echo "abcd";
        }

        location ~ ^/abcdef$ {
            echo "abcdefg";
        }

        location ~* ^/abcdef$ {
            echo "abcdef";
        }


[root@localhost nginx-1.20.1]# curl 192.168.160.150/abc
abcd
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdergh
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdef
abcdefg
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABCDEF
abcdef

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值