nginx location if 的匹配規則

nginx location if 的匹配規則

cation匹配命令php

~      #波浪線表示執行一個正則匹配,區分大小寫
~*    #表示執行一個正則匹配,不區分大小寫
^~    #^~表示普通字符匹配,不是正則匹配。若是該選項匹配,只匹配該選項,不匹配別的選項,通常用來匹配目錄
=      #進行普通字符精確匹配
@     #"@" 定義一個命名的 location,使用在內部定向時,例如 error_page, try_files
html

 

 

參考:http://www.javashuo.com/article/p-rfvnylnv-z.htmlnginx

 

 

location 優先級官方文檔正則表達式

 

1. Directives with the = prefix that match the query exactly. If found, searching stops.express

2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.segmentfault

3. Regular expressions, in order of definition in the configuration file.cookie

4. If #3 yielded a match, that result is used. Else the match from #2 is used.app

 

1. =前綴的指令嚴格匹配這個查詢。若是找到,中止搜索。ide

2. 全部剩下的常規字符串,最長的匹配。若是這個匹配使用^前綴,搜索中止。ui

3. 正則表達式,在配置文件中定義的順序。

4. 若是第3條規則產生匹配的話,結果被使用。不然,如同從第2條規則被使用。

 

順序 no優先級: (location =) > (location 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (/)

 

例如:

location  = / {   # 只匹配"/".   [ configuration A ]  } location  / {   # 匹配任何請求,由於全部請求都是以"/"開始   # 可是更長字符匹配或者正則表達式匹配會優先匹配   [ configuration B ]  } location ^~ /images/ {   # 匹配任何以 /images/ 開始的請求,並中止匹配 其它location   [ configuration C ]  } location ~* \.(gif|jpg|jpeg)$ {   # 匹配以 gif, jpg, or jpeg結尾的請求.    # 可是全部 /images/ 目錄的請求將由 [Configuration C]處理.      [ configuration D ]  }

 

 

 

個人疑問1 : 若是是如下的 

  • /images/1.gif -> 會匹配C仍是D呢?  會按順序匹配到C。由於都是正則因此按順序匹配到了C

location ~ /images/ {   # 匹配任何以 /images/ 開始的請求,並中止匹配 其它location   [ configuration C ]  } location ~* \.(gif|jpg|jpeg)$ {   # 匹配以 gif, jpg, or jpeg結尾的請求.    # 可是全部 /images/ 目錄的請求將由 [Configuration C]處理.      [ configuration D ]  }

 

 

個人疑問2: 若是是如下的。會匹配到D ,由於正則匹配到優先級大於部分起始路徑。

location  /images/ {   # 匹配任何以 /images/ 開始的請求,並中止匹配 其它location   [ configuration C ]  } location ~* \.(gif|jpg|jpeg)$ {   # 匹配以 gif, jpg, or jpeg結尾的請求.    # 可是全部 /images/ 目錄的請求將由 [Configuration C]處理.      [ configuration D ]  }

 

 

 

 

 

if 條件判斷:

參考:

http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html#if

 

語法: if (condition) { ... }
默認值:
上下文: serverlocation

計算指定的condition的值。若是爲真,執行定義在大括號中的rewrite模塊指令,並將if指令中的配置指定給請求。if指令會從上一層配置中繼承配置。

條件能夠是下列任意一種:

  • 變量名;若是變量值爲空或者是以「0」開始的字符串,則條件爲假;

  • 使用「=」和「!=」運算符比較變量和字符串;

  • 使用「~」(大小寫敏感)和「~*」(大小寫不敏感)運算符匹配變量和正則表達式。正則表達式能夠包含匹配組,匹配結果後續可使用變量$1..$9引用。若是正則表達式中包含字符「}」或者「;」,整個表達式應該被包含在單引號或雙引號的引用中。

  • 使用「-f」和「!-f」運算符檢查文件是否存在;

  • 使用「-d」和「!-d」運算符檢查目錄是否存在;

  • 使用「-e」和「!-e」運算符檢查文件、目錄或符號連接是否存在;

  • 使用「-x」和「!-x」運算符檢查可執行文件;

 

舉例:

if ($http_user_agent ~ MSIE) {     rewrite ^(.*)$ /msie/$1 break; } if ($http_cookie ~* "id=([^;]+)(?:;|$)") {     set $id $1; } if ($request_method = POST) {     return 405; } if ($slow) {     limit_rate 10k; } if ($invalid_referer) {     return 403; }

 

案例每一個用戶的guid存在cookie中要存入nginx日誌 若是存在的話:

    set $guid "-";

    if ( $http_cookie ~* "guid=(\S+)(;.*|$)"){

        set $guid $1;

    }

 

內嵌變量$invalid_referer的值是經過valid_referers指令設置的。

 

補充正則表達式知識:

參考:https://regex101.com/

(?:;|$)
(;|$)

二者的區別即 ?:的做用。加上?:在分組中表示不捕捉這個分組,在後面不能夠引用 Non-capturing group 

(?:;|$)

Capturing Group 

(;|$)

 

 

 

 

rewrite 模塊

 

重寫語法:

http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html#if

 

語法: rewrite regex replacement [flag];
默認值:
上下文: serverlocationif

若是指定的正則表達式能匹配URI,此URI將被replacement參數定義的字符串改寫。rewrite指令按其在配置文件中出現的順序執行。flag能夠終止後續指令的執行。若是replacement的字符串以「http://」或「https://」開頭,nginx將結束執行過程,並返回給客戶端一個重定向。

可選的flag參數能夠是其中之一:

  • last

  • 中止執行當前這一輪的ngx_http_rewrite_module指令集,而後查找匹配改變後URI的新location;

  • break

  • 中止執行當前這一輪的ngx_http_rewrite_module指令集;

  • redirect

  • 在replacement字符串未以「http://」或「https://」開頭時,使用返回狀態碼爲302的臨時重定向;

  • permanent

  • 返回狀態碼爲301的永久重定向。

 

 

若是URI中含有參數(/app/test.php?id=5),默認狀況下參數會被自動附加到替換串上,能夠經過在替換串的末尾加上?標記來解決這一問題。
例如:
 

複製代碼代碼示例:

rewrite ^/test(.*)$ http://www.it.net.cn/home  permanent;
訪問http://www.it.net.cn/test?id=5 會跳轉到 http://www.it.net.cn/home?id=5
 

例如:若是將相似URL /photo/123456 重定向到 /path/to/photo/12/1234/123456.png
 

複製代碼代碼示例:

Rewrite "/photo/([0-9]{2})([0-9]{2})([0-9]{2})" /path/to/photo/$1/$1$2/$1$2$3.png ;

 

 

 

set指令。這裏的變量名和php的語法差很少。變量名前面定義$表明定義變量(set)或者引用變量。

Syntax: set $variable value;
Default:
Context: serverlocationif

Sets a value for the specified variable. The value can contain text, variables, and their combination.

相關文章
相關標籤/搜索