nginx 的location的匹配顺序

匹配规则

匹配顺序

示例

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[root@python vhast]# cat test.conf
server {
        server_name haha.com;
        #listen 8080;
    rewrite_log on;
        error_page 404 /403.html;
        #return 405;
        location /{
                #return 404 "find nothing!\n";
        }
    root html/;
    location /first {
        rewrite /first(.*) /second$1 last;
        return 200 1first!\n';
        }
    location /second {
        #rewrite /second(.*) /third$1 break;
        rewrite /second(.*) /third$1 last;
        return 200 'second!\n';
        }
    location /third {
        return 200 'third!\n';
    }
    location /redirect1 {
        rewrite /redirect1(.*) $1 permanent;
    }
    location /redirect2 {
        rewrite /redirect2(.*) $1 redirect;
    }
    location /redirect3 {
        rewrite /redirect3(.*) http://haha.com:$1;
    }
    location /redirect4 {
        rewrite /redirect4(.*) http://haha.com:$1 permanent;
    }
    location ~ /Test1/$ {
        return 200 'first regular expressions match! \n';
    }
    location ~* /Test1/(\w+)$ {
        return 200 'longest regular expressions match! \n';
    }
    location ^~ /Test1/ {#禁止正则匹配
        return 200 'stop regular expressions match! \n';
    }
    location /Test1/Test2 {
        return 200 'longest prefix string match! \n';
    }
    location /Test1 {
        return 200 'prefix string match! \n';
    }      
    location = /Test1 {  #精确匹配
    return 200 'exact match!\n';
    }  
}

  测试

1
2
3
4
5
6
7
8
9
10
[root@python vhast]# curl http://haha.com/Test1
exact match!
[root@python vhast]# curl http://haha.com/Test1/
stop regular expressions match!
[root@python vhast]# curl http://haha.com/Test1/Test2
longest regular expressions match!
[root@python vhast]# curl http://haha.com/Test1/Test2/
longest prefix string match!
[root@python vhast]# curl http://haha.com/test1/Test2  #没有匹的话会记住匹配最长的那个
longest regular expressions match!

  

 

posted @   烟雨楼台,行云流水  阅读(448)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示