LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

nginx中location的匹配規則和優先級

admin
2025年6月28日 22:30 本文熱度 236

匹配規則 

  1. 精確匹配,以=開頭。如:
    location = /index.html {
     return 200 "精確匹配/index.html";
    }
  2. 普通匹配,沒有任何修飾符。如:
    location /images/ {
     return 200 "普通匹配,匹配以/images/開頭的URI";
    }

  3. 正則匹配,以~開頭,~表示區分大小寫,~*表示不區分大小寫。如:
    location ~ \.js$ {
     return 200 "匹配.js結尾的URI";
    }

    location ~* \.jpg$ {
     return 200 "匹配.JPG或.jpg結尾的URI";
    }

  4. 前綴匹配,以^~開頭,如
    location ^~ /images/ {
     return 200 "前綴匹配,匹配以/images/開頭的URI";
    }

匹配優先級 

不同匹配優先級從高到低為:

  1. =精確匹配。
  2. ^~前綴匹配。
  3. ~ ~*正則匹配,正則匹配中遇到第一個滿足的匹配就返回。
  4. 普通匹配,普通匹配中長匹配在本級中最高。

優先級測試 

一、配置規則

location /home {
    return 200 "home目錄";
}

location / {
    return 200 "根目錄";
}

location = /index.html {
    return 200 "精確匹配/index.html";
}

location ^~ /index.html {
    return 200 "前綴匹配/index.html";
}

location ^~ /content/ {
    return 200 "前綴匹配,匹配以/content/開頭的URI";
}

location ~ \.html$ {
    return 200 "正則匹配,匹配.html結尾的URI";
}

location ~ logo\.jpg$ {
    return 200 "正則匹配,匹配logo.jpg結尾的URI";
}

location ~ \.jpg$ {
    return 200 "正則匹配,匹配.jpg結尾的URI";
}

location /images/logo/ {
    return 200 "普通匹配,匹配以/images/logo/開頭的URI";
}

location /images/ {
    return 200 "普通匹配,匹配以/images/開頭的URI";
}

二、測試

  1. 驗證精確匹配優先級高于前綴匹配
    • GET http://localhost:85/index.html
    • 同時滿足/index.html^~ /index.html,返回精確匹配/index.html
  2. 驗證前綴匹配優先級高于正則匹配
    • GET http://localhost:85/content/home.html
    • 同時滿足^~ /content/~ \.html$,返回前綴匹配,匹配以/content/開頭的URI
  3. 驗證正則匹配優先級高于普通匹配
    • GET http://localhost:85/images/logo/1.jpg
    • 同時滿足~ \.jpg$/images/logo/,返回正則匹配,匹配.jpg結尾的URI
  4. 驗證正則匹配中遇到第一個滿足的匹配就返回
    • GET http://localhost:85/logo.jpg
    • 同時滿足~ logo\.jpg$~ \.jpg$,返回正則匹配,匹配logo.jpg結尾的URI
  5. 驗證正則匹配中遇到第一個滿足的匹配就返回
    • GET http://localhost:85/logo1.jpg
    • 只滿足~ \.jpg$
  6. 驗證普通匹配中長匹配在本級中最高
    • GET http://localhost:85/images/logo/1.png
    • 同時滿足/images/logo//images/,返回普通匹配,匹配以/images/logo/開頭的URI
  7. 驗證普通匹配中長匹配在本級中最高
    • GET http://localhost:85/home/1.action
    • 同時滿足/home/
  8. 精確匹配、前綴匹配、正則匹配都不滿足,僅匹配了普通匹配中最短匹配
    • GET http://localhost:85/static/1.action
    • 返回根目錄


閱讀原文:原文鏈接


該文章在 2025/7/1 23:39:46 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
中文AV免费在线 | 午夜福利在线观看爽爽爽 | 在线上观看免费a级 | 午夜福利大片免费看网址 | 制服丝袜中文字幕有码中出 | 亚洲日韩视频精品一区 |