您的位置:首页 > 运维架构 > Nginx

Nginx的location匹配规则(转载)

2014-06-30 16:23 316 查看
Nginx的Location可以有以下几个匹配:〖=〗  严格匹配这个查询。如果找到,停止搜索。〖^~ 〗 匹配路径的前缀,如果找到,停止搜索。〖~ 〗 为区分大小写的正则匹配 〖~*〗 表示不区分大小写的正则匹配
〖@〗 指定一个命名的location,一般只用于内部重定向请求。 首先对字符串进行匹配查询,最确切的匹配将被使用。然后,正则表达式的匹配查询开始,匹配第一个结果后会停止搜索,如果没有找到正则表达式,将使用字符串的搜索结果,如果字符串和正则都匹配,那么正则优先级较高。 例子:location = /{  # 只匹配对 / 目录的查询.  [ config A ]}  location /{  # 匹配以 / 开始的查询,即所有查询都匹配。  [ config B ]}  location ^~ /images/{  # 匹配以 /images/ 开始的查询,不再检查正则表达式。  [ config C ]}  location ~* \.(gif|jpg|jpeg)$ {  # 匹配以gif, jpg, or jpeg结尾的文件,但优先级低于config C。  [ config D ]} 如果要定义多个location,则可以有2种方式:使用/ :location / {  client_max_body_size 200m;  proxy_connect_timeout 30;  proxy_set_header Host $http_host;  proxy_set_header x-forwarded-for $remote_addr;  proxy_pass http://127.0.0.1:8008;}location /tmp/{  root /; internal;}采用这种方式,/tmp可以放在/的下面,因为“/是匹配任何查询,但是正则表达式规则和长的块规则将被优先和查询匹配”使用~ /* :location ~ /tmp/ {  root /tmp; internal;}location ~ /* {  client_max_body_size 20m;  proxy_connect_timeout 30;  fastcgi_pass fpass;  include fastcgi_params;}采用这种方式,/tmp则必须放在~ /*这个前面,因为~是正则匹配的,正则匹配是有顺序的,只要匹配上就不会再往下匹配了。除非在conf中有定义=或者^~,也就是说=和^~的优先级最高,如果匹配上,就不会再去匹配其它的规则了。 总之,引用Nginx的官方文档的匹配规则:Directives with the = prefix that match the query exactly. If found, searching stops.All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.Regular expressions, in order of definition in the configuration file.If #3 yielded a match, that result is used. Else the match from #2 is used.注意:正则表达式的匹配是有顺序的,按顺序匹配。其它的匹配理论上讲是只有优先级,而没有顺序的。总之:(location =) > (location 完整路径 >)>(location ^~ 路径) >(location ~* 正则)>(location 路径)只要匹配到,其它的都会忽略,然后返回到改匹配。如果都是正则,都能够匹配,以配置文件出现顺序来,谁在前谁优先。
原文地址:http://hi.baidu.com/zhang_quanyi/item/7305ae045d7423eaf55ba628
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: