nginx location 指令

 
新版翻譯
location
 
syntax: location [=|~|~*|^~|@] /uri/ { ... }
語法: location [=|~|~*|^~|@] /uri/ { ... }
 
default: no
默認:
 
context: server
上下文: server
 
This directive allows different configurations depending on the URI. It can be configured using both literal strings and regular expressions. To use regular expressions, you must use a prefix:
這個變量允許按照根據URI使用不同的配置.配置可以使用普通的字符串或者是正則表達式.使用正則表達式,必須使用一個前綴:
 
   1. "~" for case sensitive matching
   2. "~*" for case insensitive matching
   1. "~" 用於區分大小寫(大小寫敏感)的匹配
   2. "~*" 用於不區分大小寫的匹配
 
To determine which location directive matches a particular query, the literal strings are checked first. Literal strings match the beginning portion of the query - the most specific match will be used. Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the literal string search is used.
在決定哪個location變量來匹配一個特定的查詢時,普通字符串會先檢查.普通字符串查找查詢的開頭做匹配 -- 將會使用最明確的那個匹配(我的理解是:使用匹配得最完整的那個字符串的配置).然後正則表達式按照配置文件裏面的順序來匹配.第一個匹配查詢的正則表達式會停止剩下的查找.如果沒有匹配的正則表達式,就會使用普通字符串的查找結果.
 
For caseless operation systems, like Mac OS X and Cygwin, liternal string matching will be done in case insensitive way (0.7.7). However, comparision is limited to single-byte locale's only.
對於小部分系統,如Mac OS X 和Cygwin,普通字符串會以不區分大小寫的情況來做匹配(0.7.7).但是,這種區別僅限於單字節 locale的 情況.
 
Regular expression may contain captures (0.7.40), which can be used in other directives.
正則表達式可以包含capture(0.7.40),capture可以用在其他的指令中.
 
It is possible to disable regular expression checks after liternal string matching by using "^~" prefix. If most specific match literal location have this prefix - regular expressions aren't checked.
 "^~" 這個前綴的作用:在常規的字符串匹配檢查之後,不做正則表達式的檢查---即如果最明確的那個字符串匹配的location配置中有此前綴,那麼不會做正則表達式的檢查.
 
By using "=" prefix on may define exact match between URI and location. On match search stops immediately as further search has no sense. E.g. if the request "/" occurs frequently, using "location = /" will speed up processing of this request a bit as search will stop after first comparison.
使用  "=" 前綴可以做URI和location的精確匹配.匹配之後查詢就會停止.例如,如果 "/" 這個請求常出現,使用 "location = /" 將會提高一點處理這個請求的速度.
 
On exact match with literal location without "=" or "^~" prefixes search is also immediately terminated.
(如何理解翻譯?)
 
To summarize, the order in which directives are checked is as follows:
總的來說,以如下的順序來檢查指令:

   1. Directives with the "=" prefix that match the query exactly. If found, searching stops.
   1. 有 "=" 前綴的指令對"查詢"做精確的匹配,如果找到了,查找停止.
   2. All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
   2. 指令爲常規字符串.如果匹配中使用了 "^~" ,查找停止.
   3. Regular expressions, in the order they are defined in the configuration file.
   3. 指令爲正則表達式.按照配置文件裏面的順序查找.
   4. If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.
   4. 如果#3找到了,那麼就用3的.否則,就用#2的.
 
It is important to know that nginx does the comparison against decoded URIs. For example, if you wish to match "/p_w_picpaths/%20/test", then you must use "/p_w_picpaths/ /test" to determine the location.
提醒:nginx會對編碼過的URI做比較.例如,如果你想要匹配"/p_w_picpaths/%20/test" , 你必須使用 "/p_w_picpaths/ /test" 來定義這個location
 
Example:
location    = / {
    # matches the query / only.
    [ configuration A ]    
}
location    / {
    # matches any query, since all queries begin with /, but regular
    # expressions and any longer conventional blocks will be
    # matched first.
    [ configuration B ]    
}
location ^~ /p_w_picpaths/ {
    # matches any query beginning with /p_w_picpaths/ and halts searching,
    # so regular expressions will not be checked.
    [ configuration C ]    
}
location ~* \.(gif|jpg|jpeg)$ {
    # matches any request ending in gif, jpg, or jpeg. However, all
    # requests to the /p_w_picpaths/ directory will be handled by
    # Configuration C.        
    [ configuration D ]    
}
 
Example requests:
        * / -> configuration A
        * /documents/document.html -> configuration B
        * /p_w_picpaths/1.gif -> configuration C
        * /documents/1.jpg -> configuration D
 
Note that you could define these 4 configurations in any order and the results would remain the same. While nested locations are allowed by the configuration file parser, their use is discouraged and may produce unexpected results.
注意:上面的4段在文件中順序可以是任意的,出來的效果都一樣.雖然嵌套的location 指令是允許的,但不建議!會引起非預期的結果.
 
The prefix "@" specifies a named location. Such locations are not used during normal processing of requests, they are intended only to process internally redirected requests (see error_page, try_files).
 "@" 前綴指定的是一個命名的location.這樣的location在一般的請求處理中是不適用的, 僅僅設計來處理內部的重定向請求(查看error_page, try_files).
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章