欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx location語法使用介紹

 更新時間:2015年01月23日 14:36:29   投稿:mdxy-dxy  
Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用來為匹配的 URI 進(jìn)行配置,URI 即語法中的”/uri/”,可以是字符串或正則表達(dá)式。但如果要使用正則表達(dá)式,則必須指定前綴

nginx location介紹

Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用來為匹配的 URI 進(jìn)行配置,URI 即語法中的”/uri/”,可以是字符串或正則表達(dá)式。但如果要使用正則表達(dá)式,則必須指定前綴。

nginx location語法

基本語法:location [=|~|~*|^~] /uri/ { … }

= 嚴(yán)格匹配。如果這個查詢匹配,那么將停止搜索并立即處理此請求。
~ 為區(qū)分大小寫匹配(可用正則表達(dá)式)
~* 為不區(qū)分大小寫匹配(可用正則表達(dá)式)
!~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配
^~ 如果把這個前綴用于一個常規(guī)字符串,那么告訴nginx 如果路徑匹配那么不測試正則表達(dá)式。

nginx location應(yīng)用實(shí)例

location = / {
	# 只匹配 / 查詢。
	
}
location / {
	# 匹配任何查詢,因?yàn)樗姓埱蠖家?/ 開頭。但是正則表達(dá)式規(guī)則和長的塊規(guī)則將被優(yōu)先和查詢匹配。
	
}
location ^~ /images/ {
	# 匹配任何已 /images/ 開頭的任何查詢并且停止搜索。任何正則表達(dá)式將不會被測試。
	
}
location ~* \.(gif|jpg|jpeg)$ {
	# 匹配任何已 gif、jpg 或 jpeg 結(jié)尾的請求。
	
}
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è)置過期時間
  expires  1h;
  break;
}
}
 
location ~* \.(txt|doc)${ 
	#禁止訪問某個目錄
  root /data/www/wwwroot/linuxtone/test;
  deny all;
}

以下是補(bǔ)充:

Nginx Location基本語法

location

syntax: location [=|~|~*|^~] /uri/ { … }
語法: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.
這個指令隨URL不同而接受不同的結(jié)構(gòu)。你可以配置使用常規(guī)字符串和正則表達(dá)式。如果使用正則表達(dá)式,你必須使用 ~* 前綴選擇不區(qū)分大小寫的匹配或者 ~ 選擇區(qū)分大小寫的匹配。

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 指令匹配一個特定指令,常規(guī)字符串第一個測試。常規(guī)字符串匹配請求的開始部分并且區(qū)分大小寫,最明確的匹配將會被使用(查看下文明白 nginx 怎么確定它)。然后正則表達(dá)式按照配置文件里的順序測試。找到第一個比配的正則表達(dá)式將停止搜索。如果沒有找到匹配的正則表達(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.
有兩個方法修改這個行為。第一個方法是使用 “=”前綴,將只執(zhí)行嚴(yán)格匹配。如果這個查詢匹配,那么將停止搜索并立即處理這個請求。例子:如果經(jīng)常發(fā)生”/”請求,那么使用 “l(fā)ocation = /” 將加速處理這個請求。

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.
第二個是使用 ^~ 前綴。如果把這個前綴用于一個常規(guī)字符串那么告訴nginx 如果路徑匹配那么不測試正則表達(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 做比較沒有 URL 編碼,所以如果你有一個 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)格匹配這個查詢。如果找到,停止搜索。
2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
2. 剩下的常規(guī)字符串,長的在前。如果這個匹配使用 ^~ 前綴,搜索停止。
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)生匹配,則使用這個結(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)樗姓埱蠖家?/ 開頭。但是正則表達(dá)式規(guī)則和長的塊規(guī)則將被優(yōu)先和查詢匹配。
[ configuration B ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 開頭的任何查詢并且停止搜索。任何正則表達(dá)式將不會被測試。
[ 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é)尾的請求。然而所有 /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個配置結(jié)果將仍然一樣。

Nginx Location 語法,與簡單配置

一、介紹Nginx是俄羅斯人編寫的十分輕量級的HTTP服務(wù)器,Nginx,它的發(fā)音為“engine X”, 是一個高性能的HTTP和反向代理服務(wù)器,同時也是一個IMAP/POP3/SMTP 代理服務(wù)器.
二、Location語法語法:location [=|~|~*|^~] /uri/ { … }
注:
1、~   為區(qū)分大小寫匹配
2、~* 為不區(qū)分大小寫匹配
3、!~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配

示例一:

location  / { }
匹配任何查詢,因?yàn)樗姓埱蠖家?/ 開頭。但是正則表達(dá)式規(guī)則將被優(yōu)先和查詢匹配。

示例二:

location =/ {}
僅僅匹配/

示例三:

location ~* \.(gif|jpg|jpeg)$ {
rewrite \.(gif|jpg)$ /logo.png;

注:不區(qū)分大小寫匹配任何以gif,jpg,jpeg結(jié)尾的文件

三、ReWrite語法

last - 基本上都用這個Flag。
break - 中止Rewirte,不在繼續(xù)匹配
redirect - 返回臨時重定向的HTTP狀態(tài)302
permanent - 返回永久重定向的HTTP狀態(tài)301
1、下面是可以用來判斷的表達(dá)式:
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執(zhí)行
2、下面是可以用作判斷的全局變量

例:http://localhost:88/test1/test2/test.php

復(fù)制代碼 代碼如下:

$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語法

復(fù)制代碼 代碼如下:

    server {
    listen 80;
    server_name start.igrow.cn;
    index index.html index.php;
    root html;
    if ($http_host !~ "^star\.igrow\.cn$&quot  {
         rewrite ^(.*) http://star.igrow.cn$1 redirect;
    }
    }

五、防盜鏈

復(fù)制代碼 代碼如下:

location ~* \.(gif|jpg|swf)$ {
  valid_referers none blocked start.igrow.cn sta.igrow.cn;
  if ($invalid_referer) {
  rewrite ^/ http://$host/logo.png;
  }
}

六、根據(jù)文件類型設(shè)置過期時間

復(fù)制代碼 代碼如下:

location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
   expires    1h;
   break;
}
}

七、禁止訪問某個目錄

復(fù)制代碼 代碼如下:

location ~* \.(txt|doc)${
      root /data/www/wwwroot/linuxtone/test;
    deny all;
}

相關(guān)文章

最新評論