nginx location 匹配
基本形式:
location [=!~!~*!^~!/] 你的URL{
}
=
精确匹配;
~
区分大小写的正则匹配;
^~
同上。^在正则里表示已某字符串开头;
~*
不区分大小写的正则匹配;
!~
和!~*
!为取反,不匹配;
/
通用匹配,任何请求都会匹配到;
@
内部跳转用,外部不能访问
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
举个例子
比如在我们网站根目录下有一个文件help.html
,
我们的需求是:当用户访问http://abc.com/help/时
直接定位到help.html
location ^~ /help/{
root /var/www/abc;
index help.html;
}
这里是以/help/
开头的请求,都会匹配上。
http://abc.com/help/ 这个ok
http://abc.com/help/abc.html 这也ok,只要你abc.html存在
静态资源访问
如下常见的配置:
locaction ~* \.(gif|jpg|jpeg|png|js|css)${
root /var/www/abc/assets
}