首先说明:下面的英文摘自:Nginx官方
我对下文中的重要的语句进行了翻译,黄色背景是原文,深黄色背景是我的翻译。这是正确的location配置,并帮助我解开了对location的困惑和不解,在这里记录一下希望能帮助到有困难的人。
如果不想看英文:推荐两个材料 1.淘宝tengine 2.脚本之家
NGINX Plus is the complete application delivery platform for the modern web.
NGINX is the world’s most popular open source web server and load balancer for high-traffic sites, powering over 140 million properties.
NGINX Plus adds enterprise-ready features for HTTP, TCP, and UDP load balancing, such as session persistence, health checks, advanced monitoring, and management to give you the freedom to innovate without being constrained by infrastructure.
NGINX Plus can send traffic to different proxies or serve different files based on the request URIs. These blocks are defined using thelocation
directive placed within aserver
directive.
For example, you can define three location
blocks to instruct the virtual server to send some requests to one proxied server, send other requests to a different proxied server, and serve the rest of the requests by delivering files from the local file system.
NGINX Plus tests request URIs against the parameters of all location
directives and applies the directives defined in the matching location. Inside eachlocation
block, it is usually possible (with a few exceptions) to place even morelocation
directives to further refine the processing for specific groups of requests.
Note: In this guide, the word location refers to a singlelocation
context.
There are two types of parameter to thelocation
directive: prefix strings (pathnames) and regular expressions.For a request URI to match a prefix string, it must start with the prefix string.
location指令有两种类型参数,分别为 前缀字符串(路径名称)和正则表达式。
The following sample location with a pathname parameter matches request URIs that begin with/some/path/, such as /some/path/document.html. (It does not match /my-site/some/path because /some/path does not occur at the start of that URI.)
location /some/path/ {
...
}
A regular expression is preceded with the tilde (~
) for case-sensitive matching, or the tilde-asterisk (~*
) for case-insensitive matching. The following example matches URIs that include the string.html or .htm in any position.
location ~ \.html? {
...
}
To find the location that best matches a URI, NGINX Plus first compares the URI to the locations with a prefix string. It then searches the locations with a regular expression.
为了找出最优匹配URI的location,Nginx Plus首先通过请求URI与带有前缀字符串的location进行比较,然后再搜索匹配带有正则表达式的locations.
Higher priority is given to regular expressions, unless the^~
modifier is used. Among the prefix strings NGINX Plus selects the most specific one (that is, the longest and most complete string). The exact logic for selecting a location to process a request is given below:
正则表达式具有较高优先级,除非^~
修饰符被使用。在前缀字符串中,Nginx Plus选择最精确匹配的一个(即,最长并且完全匹配的)。下面是如何选择一个location去处理一个请求的精确逻辑:
- Test the URI against all prefix strings.
- The
=
(equals sign) modifier defines an exact match of the URI and a prefix string. If the exact match is found, the search stops.
- If the
^~
(caret-tilde) modifier prepends the longest matching prefix string, the regular expressions are not checked.
- Store the longest matching prefix string.
- Test the URI against regular expressions.
- Break on the first matching regular expression and use the corresponding location.
- If no regular expression matches, use the location corresponding to the stored prefix string.
1. 对所有的前缀字符串去测试URI
2. = 等号标识符定义了URI与前缀字符串的精确匹配。如果这个精确的匹配找到,那么搜索结束。
3. 如果^~
标识符前置于最长匹配的前缀字符串,那么正则表达式将不会被检查。(自己的理解-->>>即也就是 省略了后续的5,6,7三个步骤,所以就结束了)
4. 存储最长匹配的前缀字符串
5. 对所有的正则表达式去测试URI
6. 如果找到第一个匹配的正则表达式则中断搜索,并使用相应的location
7. 如果没有匹配的正则表达式,那么就使用已存储的前缀表达式相应的location
A typical use case for the =
modifier is requests for / (forward slash). If requests for/ are frequent, specifying = /
as the parameter to thelocation
directive speeds up processing, because the search for matches stops after the first comparison.
location = / {
...
}
A location
context can contain directives that define how to resolve a request – either serve a static file or pass the request to a proxied server. In the following example, requests that match the firstlocation
context are served files from the /data directory and the requests that match the second are passed to the proxied server that hosts content for thewww.example.com domain.
server {
location /images/ {
root /data;
}
location / {
proxy_pass http://www.example.com;
}
}
The root
directive specifies the file system path in which to search for the static files to serve. The request URI associated with the location is appended to the path to obtain the full name of the static file to serve. In the example above, in response to a request for/images/example.png, NGINX Plus delivers the file/data/images/example.png.
The proxy_pass
directive passes the request to the proxied server accessed with the configured URL. The response from the proxied server is then passed back to the client. In the example above, all requests with URIs that do not start with/images/ are be passed to the proxied server.