原文链接:http://nginx.org/en/docs/http/ngx_http_core_module.html

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:
Context: serverlocation
项目 说明 优先级
= 精确匹配 1
  模糊匹配 4
区分大小写 3
~* 不分区大小写 3
^~ 从开头部分匹配 2

Sets configuration depending on a request URI.

The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “..”, and possible compression of two or more adjacent slashes into a single slash.

A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.

location blocks can be nested, with some exceptions mentioned below.

For case-insensitive operating systems such as macOS and Cygwin, matching with prefix strings ignores a case (0.7.7). However, comparison is limited to one-byte locales.

Regular expressions can contain captures (0.7.40) that can later be used in other directives.

If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.

Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.

In versions from 0.7.1 to 0.8.41, if a request matched the prefix location without the “=” and “^~” modifiers, the search also terminated and regular expressions were not checked.

Let’s illustrate the above by an example:

location = / {
[ configuration A ]
} location / {
[ configuration B ]
} location /documents/ {
[ configuration C ]
} location ^~ /images/ {
[ configuration D ]
} location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}

1.当URI是“/index.html”时,匹配B
2.当URI是“/documents/document.html”时,匹配C
3.当URI是“/images/1.gif”时,匹配D
4.当URI是“/documents/1.jpg”时,匹配E
5.当URI是“/”时,匹配A

The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.

The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_passfastcgi_passuwsgi_passscgi_pass, or memcached_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:

location /user/ {
proxy_pass http://user.example.com;
} location = /user {
proxy_pass http://login.example.com;
}

使用模糊匹配时,如果URI最后没有加上“/”,在跳转的时候会自在URI后面加上“/”。如果不想这样,可以使用上面两种方法。

最新文章

  1. C#调用SendMessage 用法
  2. JAVA中使用DOM解析XML文件
  3. .Learning.Python.Design.Patterns.2nd.Edition之单实例模式
  4. Java递归搜索指定文件夹下的匹配文件
  5. CSS兼容问题大全
  6. HDU 4727 The Number Off of FFF 2013年四川省赛题
  7. jQuery跳房子插件hopscotch
  8. hdu 5573Binary Tree
  9. SpringMvc+Spring+MyBatis 基于注解整合
  10. 开源摄影机:Axiom Camera
  11. elementUi源码解析(1)--项目结构篇
  12. 02.02.01 第1章 简介及基础操作(Power BI商业智能分析)
  13. 饮冰三年-人工智能-linux-08 软件包管理(Python的安装)
  14. Painting the Fence Gym - 101911E(构造)
  15. cdnbest设置301跳转
  16. python 字符串内置方法实例
  17. ZOJ_3950_How Many Nines 解题报告及如何对程序进行测试修改
  18. bzoj 2809 可并堆维护子树信息
  19. 【struts2】名为dispatcher的ResultType
  20. day18(JDBC事务&连接池介绍&DBUtils工具介绍&BaseServlet作用)

热门文章

  1. 【bzoj 4455】小星星(树型DP+容斥原理+dfs建树和计算的2种方式)
  2. AtCoder Beginner Contest 181 E - Transformable Teacher (贪心,二分)
  3. Codeforces Round #603 C. Everyone is a Winner!
  4. Codeforces Round#630 div2 A~C题解
  5. Codeforces Round #650 (Div. 3) E. Necklace Assembly (暴力)
  6. rabbitmq学习二
  7. Playbook 角色(Roles) 和 Include 语句
  8. NGINX configure auto generator
  9. cnblogs & 502 Bad Gateway
  10. code magic