Ngnix location匹配規則

Ngnix 站點:http://www.nginx.cnhtml

Location 匹配命令nginx

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

Location優先級正則表達式

Location優先級
Directives with the = prefix that match the query exactly. If found, searching stops.
All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
Regular expressions, in order of definition in the configuration file.
If #3 yielded a match, that result is used. Else the match from #2 is used.
=前綴的指令嚴格匹配這個查詢。若是找到,中止搜索。
全部剩下的常規字符串,最長的匹配。若是這個匹配使用^〜前綴,搜索中止。
正則表達式,在配置文件中定義的順序。
若是第3條規則產生匹配的話,結果被使用。不然,如同從第2條規則被使用。


例如express

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 ] 
}

請求URI例子:fetch

  • / -> 符合configuration A
  • /documents/document.html -> 符合configuration B
  • /images/1.gif -> 符合configuration C
  • /documents/1.jpg ->符合 configuration D

@location 例子
   error_page 404 = @fetch;this

   location @fetch(
      proxy_pass http://fetch;
   )spa

 

原文地址:http://www.nginx.cn/115.html code

相關文章
相關標籤/搜索