1. 程式人生 > 實用技巧 >nginx location標籤的匹配規則

nginx location標籤的匹配規則



location的匹配


匹配符 匹配規則 優先順序
= 精確匹配 1
^~ 以某個字串開頭 2
~ 區分大小寫的正則匹配 3
~* 不區分大小寫的正則匹配 4
!~ 區分大小寫不匹配的正則 5
!~* 不區分大小寫不匹配的正則 6
/ 通用匹配,任何請求都會匹配到 7
# 通用匹配,任何請求都會匹配到
location / {
        default_type text/html;
        return 200 "訪問到了 /";
}
 
# 嚴格區分大小寫,匹配以.php結尾的都走這個location    
location ~ \.php$ {
        default_type text/html;
        return 200 "訪問到了 php結尾的檔案";    
}
 
# 嚴格區分大小寫,匹配以.jsp結尾的都走這個location 
location ~ \.jsp$ {
        default_type text/html;
        return 200 "訪問到了 .jsp結尾的檔案";   
}
 
# 不區分大小寫匹配,只要使用者訪問.jpg,gif,png,js,css 都走這條location
location ~* \.(jpg|gif|png|js|css)$ {
        default_type text/html;
        return 200 "訪問到了 .jpg結尾的檔案";   
}
 
# 不區分大小寫匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
        default_type text/html;
        return 200 "訪問到了 .tar.gz結尾的檔案";   
}



FBI WARNING

QQ:1402122292 認準原創sheldon 別人叫我曉東