Configuring Locations(配置locations)

首先說明:下面的英文摘自: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去處理一個請求的精確邏輯:

  1. Test the URI against all prefix strings.
  2. The= (equals sign) modifier defines an exact match of the URI and a prefix string. If the exact match is found, the search stops.
  3. If the^~ (caret-tilde) modifier prepends the longest matching prefix string, the regular expressions are not checked.
  4. Store the longest matching prefix string.
  5. Test the URI against regular expressions.
  6. Break on the first matching regular expression and use the corresponding location.
  7. 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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章