Nginx location 匹配规则

匹配模式及顺序

=   用于标准uri前,需要请求字串与uri完全匹配,如果匹配成功就停止向下匹配并立即处理请求。
~   区分大小写
~*   不区分大写

^~     开头对URL路径进行前缀匹配,并且在正则之前

!~    区分大小写不匹配
!~*  不区分大小写不匹配
^   匹配正则开头
$   匹配正则结尾
\   转义字符。可以转. * ?等
*   代表任意长度的任意字符

 

#不区分大小写匹配任何以gif、jpg、jpeg结尾的请求
location ~* .(gif|jpg|jpeg)$ {  
     
}

#区分大小写匹配以.txt结尾的请求,并设置此location的路径是/usr/local/nginx/html/。也就是以.txt结尾的请求将访问/usr/local/nginx/html/ 路径下的txt文件
location ~ ^.+\.txt$ {
    root /usr/local/nginx/html/;
}

# ^~ 以 /admin/ 开头的请求,都会匹配上
location ^~ /admin/ {
   root /xvdb/mobai/
}

此时 /xvdb/mobai/目录中必须要有 admin 文件夹

#匹配成功: http://www.cnblogs.com/admin/index.jsp
#匹配成功: http://www.cnblogs.com/admin/login.jsp


#以 /dev_and_test 都会被匹配上,并且跳转到https://ng.cnblog.com
location ^~ /dev_and_test {
rewrite ^/dev_and_test/(.*)$ https://ng.cnblog.com/$1 permanent;
}
#匹配成功: http://www.cnblogs.com/dev_and_test/index.jsp 并且跳转到 https://ng.cnblog.com/index.jsp



#忽略大小写,包含/Images/
location ~* /Images/ {
}
#匹配成功: http://www.cnblogs.com/test/Images/     
#匹配成功: http://www.cnblogs.com/images/    


#不加任何规则则时,默认是大小写敏感,前缀匹配,相当于加了“^”与“~”
location /index/ {
}
#匹配成功: http://www.cnblogs.com/index
#匹配成功: http://www.cnblogs.com/index/index.html
#匹配失败: http://www.cnblogs.com/test/index
#匹配失败: http://www.cnblogs.com/Index

 
#精确匹配, 只匹配http://www.cnblogs.com
location = / {
}
#匹配成功: http://www.cnblogs.com 
#匹配失败: http://www.cnblogs.com/index

# 匹配到所有url
location / {

}

 

posted on 2024-03-07 09:47  星河赵  阅读(44)  评论(0编辑  收藏  举报

导航