本文章轉自:http://www.linuxtone.org/thread-1069-1-1.htmlhtml
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 conventional strings and regular expressions. To use regular expressions, you must use the prefix ~* for case insensitive match and ~ for case sensitive match.
這個指令隨URL不一樣而接受不一樣的結構。你能夠配置使用常規字符串和正則表達式。若是使用正則表達式,你必須使用 ~* 前綴選擇不區分大小寫的匹配或者 ~ 選擇區分大小寫的匹配。
To determine which location directive matches a particular query, the conventional strings are checked first. Conventional strings match the beginning portion of the query and are case-sensitive - the most specific match will be used (see below on how nginx determines this). 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 convention string search is used.
肯定 哪一個location 指令匹配一個特定指令,常規字符串第一個測試。常規字符串匹配請求的開始部分而且區分大小寫,最明確的匹配將會被使用(查看下文明白 nginx 怎麼肯定它)。而後正則表達式按照配置文件裏的順序測試。找到第一個比配的正則表達式將中止搜索。若是沒有找到匹配的正則表達式,使用常規字符串的結果。
There are two ways to modify this behavior. The first is to use the prefix 「=」, which matches an exact query only. If the query matches, then searching stops and the request is handled immediately. For example, if the request 「/」 occurs frequently, then using 「location = /」 will expedite the processing of this request.
有兩個方法修改這個行爲。第一個方法是使用 「=」前綴,將只執行嚴格匹配。若是這個查詢匹配,那麼將中止搜索並當即處理這個請求。例子:若是常常發生」/」請求,那麼使用 「location = /」 將加速處理這個請求。
The second is to use the prefix ^~. This prefix is used with a conventional string and tells nginx to not check regular expressions if the path provided is a match. For instance, 「location ^~ /images/」 would halt searching if the query begins with /images/ - all regular expression directives would not be checked.
第二個是使用 ^~ 前綴。若是把這個前綴用於一個常規字符串那麼告訴nginx 若是路徑匹配那麼不測試正則表達式。
Furthermore it is important to know that NGINX does the comparison not URL encoded, so if you have a URL like 「/images/%20/test」 then use 「/images/ /test」 to determine the location.
並且它重要在於 NGINX 作比較沒有 URL 編碼,因此若是你有一個 URL 連接’/images/%20/test’ , 那麼使用 「images/ /test」 限定location。
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, longest match first. If this match used the ^~ prefix, searching stops.
2. 剩下的常規字符串,長的在前。若是這個匹配使用 ^~ 前綴,搜索中止。
3. Regular expressions, in order of definition in the configuration file.
3. 正則表達式,按配置文件裏的順序。
4. If #3 yielded a match, that result is used. Else the match from #2 is used.
4. 若是第三步產生匹配,則使用這個結果。不然使用第二步的匹配結果。
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 ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 開頭的任何查詢而且中止搜索。任何正則表達式將不會被測試。
[ configuration C ]
}
location ~* ".(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration C.
# 匹配任何已 gif、jpg 或 jpeg 結尾的請求。然而全部 /images/ 目錄的請求將使用 Configuration C。
[ configuration D ]
}
Example requests:
例子請求:
*
/ -> configuration A
*
/documents/document.html -> configuration B
*
/images/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.
注意:按任意順序定義這4個配置結果將仍然同樣。linux