2011-03-08 58 views

回答

205

HttpCoreModule docs

  1. 指令與 「=」 前綴完全匹配查詢。如果找到,搜索停止。
  2. 使用常規字符串的所有其餘指令。如果此匹配使用「^〜」前綴,則停止搜索。
  3. 正則表達式,按它們在配置文件中定義的順序排列。
  4. 如果#3產生匹配,則使用該結果。否則,使用來自#2的匹配。從文檔

例子:

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 /documents/ { 
    # matches any query beginning with /documents/ and continues searching, 
    # so regular expressions will be checked. This will be matched only if 
    # regular expressions don't find a match. 
    [ configuration C ] 
} 
location ^~ /images/ { 
    # matches any query beginning with /images/ and halts searching, 
    # so regular expressions will not be checked. 
    [ configuration D ] 
} 
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 D. 
    [ configuration E ] 
} 

如果它仍然混亂,here's a longer explanation

+0

@brablc謝謝,固定。 – 2015-05-11 21:40:03

+4

與它可以幫助你:)https://github.com/detailyang/nginx-location-match-visible – user2228392 2016-08-01 07:33:36

+3

請注意,'/'和'/ documents /'規則匹配請求'/ documents/index.html' ,但後者的規則優先,因爲它是最長的規則。 – 2017-08-13 19:23:29

11

按此順序點亮。

  1. =(精確地):位置= /路徑
  2. ^〜(正向匹配):位置^〜/路徑
  3. 〜(正則表達式區分大小寫):位置〜/路徑/
  4. 〜*(正則表達式不區分大小寫):位置〜*(JPG | PNG | BMP)
  5. /:位置/路徑