location优先级
# 匹配符 匹配规则 优先级
# = 精确匹配 1
# ^~ 以某个字符串开头 2
# ~ 区分大小写的正则匹配 3
# ~* 不区分大小写的正则匹配 4
# / 通用匹配,任何请求都会匹配到 5
#测试结果如下(建议是curl测试)
#1.请求 http://location.oldxu.com/ 会被 location =/ 匹配
#2.请求 http://location.oldxu.com/index.html 会被 location / 匹配
#3.请求 http://location.oldxu.com/documents/1.html 会被 location /documents/ 匹配
#4.请求 http://location.oldxu.com/images/1.gif 会被 location ^~ /images/ 匹配
#5.请求 http://location.oldxu.com/documents/1.jpg 会被 location ~* .(gif|jpg|jpeg)$匹配
[root@web01 conf.d]# cat location2.oldxu.com.conf
server {
listen 80;
server_name location2.oldxu.com;
# 通用匹配,任何请求都会匹配到
location / {
root html;
index index.html;
}
# 精准匹配,必须请求的uri是/nginx_status
location = /nginx_status {
stub_status;
}
# 严格区分大小写,匹配以.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)$ {
return 403;
}
# 不区分大小写匹配
location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
deny all;
}
}