nginx的location配置
1.语法
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
2.location匹配符
匹配符 | 匹配规则 | 优先级 |
---|
= | 精确匹配 | 1 |
^~ | 以某个字符串开头 | 2 |
~ | 区分大小写的正则匹配 | 3 |
~* | 不区分大小写的正则匹配 | 3 |
/ | 通用匹配,任何请求都会匹配到 | 4 |
3.优先级验证
[root@web01 ~]
server {
listen 80;
server_name linux.test.com;
location / {
default_type text/html;
return 200 "location /";
}
location =/ {
default_type text/html;
return 200 "location =/";
}
location ~ / {
default_type text/html;
return 200 "location ~/";
}
}
4.Locaiton应用场景
location / {
...
}
location ~ \.php$ {
...
}
location ~ \.jsp$ {
...
}
location ~* .*\.(jpg|gif|png|js|css)$ {
...
}
http://linux.test.com/1.PHP
http://linux.test.com/1.JPG
http://linux.test.com/1.jsp
http://linux.test.com/1.Gif
http://linux.test.com/1.PnG
http://linux.test.com/1.JsP