nginx location标签的匹配规则



location的匹配


匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 4
!~ 区分大小写不匹配的正则 5
!~* 不区分大小写不匹配的正则 6
/ 通用匹配,任何请求都会匹配到 7
# 通用匹配,任何请求都会匹配到
location / {
        default_type text/html;
        return 200 "访问到了 /";
}
 
# 严格区分大小写,匹配以.php结尾的都走这个location    
location ~ \.php$ {
        default_type text/html;
        return 200 "访问到了 php结尾的文件";    
}
 
# 严格区分大小写,匹配以.jsp结尾的都走这个location 
location ~ \.jsp$ {
        default_type text/html;
        return 200 "访问到了 .jsp结尾的文件";   
}
 
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* \.(jpg|gif|png|js|css)$ {
        default_type text/html;
        return 200 "访问到了 .jpg结尾的文件";   
}
 
# 不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
        default_type text/html;
        return 200 "访问到了 .tar.gz结尾的文件";   
}



posted @ 2020-07-14 20:00  Gshelldon  阅读(343)  评论(0编辑  收藏  举报