Location区段
Location区段
是通过指定模式来与客户端请求的URI相匹配,基本语法如:location [=|~|~*|^~|@] pattern{……}
修饰符 | 说明 | 语法示例 | 匹配的URI |
---|---|---|---|
没有修饰符 | 必须以指定模式开始 | server { server_name test.com; location /abc { …… } } |
http://test.com/abc http://test.com/abc?p1=11&p2=22 http://test.com/abc/ http://test.com/abcde |
= | 必须与指定的模式精确匹配 | server { server_name test.com; location = /abc { …… } } |
http://test.com/abc http://test.com/abc?p1=11&p2=22 |
~ | 指定的正则表达式要区分大小写 | server { server_name test.com; location ~ ^/abc$ { …… } } |
http://test.com/abc http://test.com/abc?p1=11&p2=22 |
~* | 指定的正则表达式不区分大小写 | server { server_name test.com; location ~* ^/abc$ { …… } } |
http://test.com/abc http://test.com/ABC http://test.com/abc?p1=11&p2=22 |
^~ | 类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了。 | ||
@ | 定义命名location区段,这些区段客户段不能访问,只可以由内部产生的请求来访问,如try_files或error_page等。 |
查找顺序和优先级
- 带有“=“的精确匹配优先。
- 没有修饰符的精确匹配。
- 正则表达式按照他们在配置文件中定义的顺序。
- 带有“^~”修饰符的,开头匹配。
- 带有“~” 或“~*” 修饰符的,如果正则表达式与URI匹配。
- 没有修饰符的,如果指定字符串与URI开头匹配。