1、location的语法规则
location [=|^~|~|~*] /url/ …
符号 | 含义 |
---|---|
= | 表示精确匹配,输入的url名必须相同 |
^~ | 表示匹配以某个字符串开头的url路径 |
~ | 表示匹配区分大小写的url字符正则表达式 |
~* | 表示匹配不区分字符串大小写的url正则表达式 |
/filename | 表示匹配此url路径下的的文件资源 |
/ | 通用匹配,任何的请求都会被匹配到 |
2、location匹配的优先级
(=) >(^~) > (~) >(~*) > (/filename) >(/)
3、匹配案例演示:
location = /aaa/ {
root /nginx/web/;
index index.html; //这是精确匹配,只能是输入/aaa/才会被匹配到的资源;
}
location ^~ /bbb/ {
root /nginx/web;
index index.html; //这是以/bbb/资源开头的资源,访问的路径为/nginx/web/bbb/index.html;
}
location ~ \.(jpg|gif|png)$ {