location表达式介绍:
~ #表示正则匹配,区分大小写
~* #表示正则匹配,不区分大小写
^~ #表示匹配常规字符串,不进行正则匹配
= #表示精准匹配,只能匹配=后面的内容,且不继续向下匹配
location表达式优先级:
第一优先级:“=” 精确匹配,一旦匹配成功,不再继续匹配
第二优先级:“^~”普通字符匹配,一旦匹配成功,不再继续匹配
第三优先级:“~”和“~*”正则表达式,如果多个location正则匹配,优先匹配最长
第四优先级:常规字符串匹配
例子:
location = / {
return 401;
}
location = /documents/ {
return 402;
}
location ^~ /documents/ {
return 403;
}
location /images/ {
return 405;
}
location ~* \.(gif|jpg|jepg)$ {
return 406;
}
结果:
1、http://xxx.com/ ===> 401
2、http://xxx.com/documents/ ===> 402
3、http://xxx.com/documents/aaa ===> 403
4、http://xxx.com/images/ ===> 405
5、http://xxx.com/images/1.jpg ===> 406
6、http://xxx.com/documents/1.jpg ===> 403
7、http://xxx.com/asdada/1.jpg ===> 406
proxy_pass 地址后带“/”和不带“/”:
例1:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
结果:请求 http://xxx.com/name/test.html
实际访问的是 http://127.0.0.1/remote/test.html
例2:
location /name/ {
proxy_pass http://127.0.0.1;
}
结果:请求 http://xxx.com/name/test.html
实际访问的是 http://127.0.0.1/name/test.html