nginx location語(yǔ)法使用介紹
nginx location介紹
Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用來(lái)為匹配的 URI 進(jìn)行配置,URI 即語(yǔ)法中的”/uri/”,可以是字符串或正則表達(dá)式。但如果要使用正則表達(dá)式,則必須指定前綴。
nginx location語(yǔ)法
基本語(yǔ)法:location [=|~|~*|^~] /uri/ { … }
= 嚴(yán)格匹配。如果這個(gè)查詢匹配,那么將停止搜索并立即處理此請(qǐng)求。
~ 為區(qū)分大小寫(xiě)匹配(可用正則表達(dá)式)
~* 為不區(qū)分大小寫(xiě)匹配(可用正則表達(dá)式)
!~和!~*分別為區(qū)分大小寫(xiě)不匹配及不區(qū)分大小寫(xiě)不匹配
^~ 如果把這個(gè)前綴用于一個(gè)常規(guī)字符串,那么告訴nginx 如果路徑匹配那么不測(cè)試正則表達(dá)式。
nginx location應(yīng)用實(shí)例
location = / { # 只匹配 / 查詢。 }
location / { # 匹配任何查詢,因?yàn)樗姓?qǐng)求都已 / 開(kāi)頭。但是正則表達(dá)式規(guī)則和長(zhǎng)的塊規(guī)則將被優(yōu)先和查詢匹配。 }
location ^~ /images/ { # 匹配任何已 /images/ 開(kāi)頭的任何查詢并且停止搜索。任何正則表達(dá)式將不會(huì)被測(cè)試。 }
location ~* \.(gif|jpg|jpeg)$ { # 匹配任何已 gif、jpg 或 jpeg 結(jié)尾的請(qǐng)求。 }
location ~* \.(gif|jpg|swf)$ { valid_referers none blocked start.igrow.cn sta.igrow.cn; if ($invalid_referer) { #防盜鏈 rewrite ^/ http://$host/logo.png; } }
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ { if (-f $request_filename) { #根據(jù)文件類型設(shè)置過(guò)期時(shí)間 expires 1h; break; } }
location ~* \.(txt|doc)${ #禁止訪問(wèn)某個(gè)目錄 root /data/www/wwwroot/linuxtone/test; deny all; }
以下是補(bǔ)充:
Nginx Location基本語(yǔ)法
location
syntax: location [=|~|~*|^~] /uri/ { … }
語(yǔ)法:location [=|~|~*|^~] /uri/ { … }
default: no
默認(rèn):否
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.
這個(gè)指令隨URL不同而接受不同的結(jié)構(gòu)。你可以配置使用常規(guī)字符串和正則表達(dá)式。如果使用正則表達(dá)式,你必須使用 ~* 前綴選擇不區(qū)分大小寫(xiě)的匹配或者 ~ 選擇區(qū)分大小寫(xiě)的匹配。
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.
確定 哪個(gè)location 指令匹配一個(gè)特定指令,常規(guī)字符串第一個(gè)測(cè)試。常規(guī)字符串匹配請(qǐng)求的開(kāi)始部分并且區(qū)分大小寫(xiě),最明確的匹配將會(huì)被使用(查看下文明白 nginx 怎么確定它)。然后正則表達(dá)式按照配置文件里的順序測(cè)試。找到第一個(gè)比配的正則表達(dá)式將停止搜索。如果沒(méi)有找到匹配的正則表達(dá)式,使用常規(guī)字符串的結(jié)果。
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 “l(fā)ocation = /” will expedite the processing of this request.
有兩個(gè)方法修改這個(gè)行為。第一個(gè)方法是使用 “=”前綴,將只執(zhí)行嚴(yán)格匹配。如果這個(gè)查詢匹配,那么將停止搜索并立即處理這個(gè)請(qǐng)求。例子:如果經(jīng)常發(fā)生”/”請(qǐng)求,那么使用 “l(fā)ocation = /” 將加速處理這個(gè)請(qǐng)求。
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, “l(fā)ocation ^~ /images/” would halt searching if the query begins with /images/ - all regular expression directives would not be checked.
第二個(gè)是使用 ^~ 前綴。如果把這個(gè)前綴用于一個(gè)常規(guī)字符串那么告訴nginx 如果路徑匹配那么不測(cè)試正則表達(dá)式。
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 做比較沒(méi)有 URL 編碼,所以如果你有一個(gè) URL 鏈接'/images/%20/test' , 那么使用 “images/ /test” 限定location。
To summarize, the order in which directives are checked is as follows:
總結(jié),指令按下列順序被接受:
1. Directives with the = prefix that match the query exactly. If found, searching stops.
1. = 前綴的指令嚴(yán)格匹配這個(gè)查詢。如果找到,停止搜索。
2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
2. 剩下的常規(guī)字符串,長(zhǎng)的在前。如果這個(gè)匹配使用 ^~ 前綴,搜索停止。
3. Regular expressions, in order of definition in the configuration file.
3. 正則表達(dá)式,按配置文件里的順序。
4. If #3 yielded a match, that result is used. Else the match from #2 is used.
4. 如果第三步產(chǎn)生匹配,則使用這個(gè)結(jié)果。否則使用第二步的匹配結(jié)果。
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.
# 匹配任何查詢,因?yàn)樗姓?qǐng)求都已 / 開(kāi)頭。但是正則表達(dá)式規(guī)則和長(zhǎng)的塊規(guī)則將被優(yōu)先和查詢匹配。
[ configuration B ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 開(kāi)頭的任何查詢并且停止搜索。任何正則表達(dá)式將不會(huì)被測(cè)試。
[ 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 結(jié)尾的請(qǐng)求。然而所有 /images/ 目錄的請(qǐng)求將使用 Configuration C。
[ configuration D ]
}
Example requests:
例子請(qǐng)求:
*
/ -> 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個(gè)配置結(jié)果將仍然一樣。
Nginx Location 語(yǔ)法,與簡(jiǎn)單配置
一、介紹Nginx是俄羅斯人編寫(xiě)的十分輕量級(jí)的HTTP服務(wù)器,Nginx,它的發(fā)音為“engine X”, 是一個(gè)高性能的HTTP和反向代理服務(wù)器,同時(shí)也是一個(gè)IMAP/POP3/SMTP 代理服務(wù)器.
二、Location語(yǔ)法語(yǔ)法:location [=|~|~*|^~] /uri/ { … }
注:
1、~ 為區(qū)分大小寫(xiě)匹配
2、~* 為不區(qū)分大小寫(xiě)匹配
3、!~和!~*分別為區(qū)分大小寫(xiě)不匹配及不區(qū)分大小寫(xiě)不匹配
示例一:
location / { }
匹配任何查詢,因?yàn)樗姓?qǐng)求都以 / 開(kāi)頭。但是正則表達(dá)式規(guī)則將被優(yōu)先和查詢匹配。
示例二:
location =/ {}
僅僅匹配/
示例三:
location ~* \.(gif|jpg|jpeg)$ {
rewrite \.(gif|jpg)$ /logo.png;
}
注:不區(qū)分大小寫(xiě)匹配任何以gif,jpg,jpeg結(jié)尾的文件
三、ReWrite語(yǔ)法
last - 基本上都用這個(gè)Flag。
break - 中止Rewirte,不在繼續(xù)匹配
redirect - 返回臨時(shí)重定向的HTTP狀態(tài)302
permanent - 返回永久重定向的HTTP狀態(tài)301
1、下面是可以用來(lái)判斷的表達(dá)式:
-f和!-f用來(lái)判斷是否存在文件
-d和!-d用來(lái)判斷是否存在目錄
-e和!-e用來(lái)判斷是否存在文件或目錄
-x和!-x用來(lái)判斷文件是否可執(zhí)行
2、下面是可以用作判斷的全局變量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
四、Redirect語(yǔ)法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ "^star\.igrow\.cn$" {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
五、防盜鏈
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
六、根據(jù)文件類型設(shè)置過(guò)期時(shí)間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
七、禁止訪問(wèn)某個(gè)目錄
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
相關(guān)文章
PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯(cuò)誤的解決方法
這篇文章主要介紹了PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯(cuò)誤的解決方法,涉及nginx平臺(tái)的相關(guān)配置技巧,需要的朋友可以參考下2016-05-05詳解Nginx服務(wù)器的配置中開(kāi)啟文件Gzip壓縮的方法
這篇文章主要介紹了Nginx服務(wù)器的配置中開(kāi)啟文件Gzip壓縮的方法,可以對(duì)CSS和JavaScript以及各種圖片等web傳輸?shù)奈募M(jìn)行壓縮,需要的朋友可以參考下2016-01-01nginx訪問(wèn)控制的實(shí)現(xiàn)示例
這篇文章主要介紹了nginx訪問(wèn)控制的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11ubuntu上配置Nginx+PHP5 FastCGI服務(wù)器配置
ubuntu上配置Nginx+PHP5 FastCGI服務(wù)器配置方法, 需要的朋友可以參考下。2010-06-06Nginx服務(wù)器作反向代理時(shí)的緩存配置要點(diǎn)解析
這篇文章主要介紹了Nginx服務(wù)器作反向代理時(shí)的緩存配置要點(diǎn)解析,需要的朋友可以參考下2016-04-04Nginx如何配置Http、Https、WS、WSS的方法步驟
這篇文章主要介紹了Nginx如何配置Http、Https、WS、WSS的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05用nginx搭建簡(jiǎn)單的文件下載服務(wù)器的方法
本篇文章主要介紹了用nginx搭建簡(jiǎn)單的文件下載服務(wù)器的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01