1.1 Location規則
語法規則: location [=|~|~*|^~] /uri/ {… }
首先匹配 =,其次匹配^~,其次是按文件中順序的正則匹配,最后是交給 /通用匹配。當有匹配成功時候,停止匹配,按當前匹配規則處理請求。
| 符號 |
含義 |
| = |
= 開頭表示精確匹配 |
| ^~ |
^~開頭表示uri以某個常規字符串開頭,理解為匹配 url路徑即可。nginx不對url做編碼,因此請求為/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格) |
| ~ |
~ 開頭表示區分大小寫的正則匹配 |
| ~* |
~* 開頭表示不區分大小寫的正則匹配 |
| !~和!~* |
!~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配的正則 |
| / |
用戶所使用的代理(一般為瀏覽器) |
| $http_x_forwarded_for |
可以記錄客戶端IP,通過代理服務器來記錄客戶端的ip地址 |
| $http_referer |
可以記錄用戶是從哪個鏈接訪問過來的 |
匹配規則示例:
location = / {
#規則A
}
location = /login {
#規則B
}
location ^~ /static/ {
#規則C
}
location ~ \.(gif|jpg|png|js|css)$ {
#規則D
}
location ~* \.png$ {
#規則E
}
location !~ \.xhtml$ {
#規則F
}
location !~* \.xhtml$ {
#規則G
}
location / {
#規則H
}
那么產生的效果如下:
1. 訪問根目錄/,比如http://localhost/將匹配規則A
2. 訪問 http://localhost/login 將匹配規則B,http://localhost/register則匹配規則H
3. 訪問 http://localhost/static/a.html 將匹配規則C
4. 訪問 http://localhost/a.gif,http://localhost/b.jpg 將匹配規則D和規則E,但是規則D順序優先,規則E不起作用,而http://localhost/static/c.png則優先匹配到規則C
5. 訪問 http://localhost/a.PNG 則匹配規則E,而不會匹配規則D,因為規則E不區分大小寫。
6. 訪問 http://localhost/a.xhtml 不會匹配規則F和規則G,http://localhost/a.XHTML不會匹配規則G,因為不區分大小寫。規則F,規則G屬於排除法,符合匹配規則但是不會匹配到,所以想想看實際應用中哪里會用到。
7. 訪問 http://localhost/category/id/1111 則最終匹配到規則H,因為以上規則都不匹配,這個時候應該是nginx轉發請求給后端應用服務器,比如FastCGI(php),tomcat(jsp),nginx作為方向代理服務器存在。
1.2 實際常用規則
#直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理。
#這里是直接轉發給后端應用服務器了,也可以是一個靜態首頁
# 第一個必選規則
location = / {
proxy_passhttp://tomcat:8080/index
}
# 第二個必選規則是處理靜態文件請求,這是nginx作為http服務器的強項
# 有兩種配置模式,目錄匹配或后綴匹配,任選其一或搭配使用
location ^~ /static/ {
# 請求/static/a.txt 將被映射到實際目錄文件:/webroot/res/static/a.txt
root /webroot/res/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)${
root /webroot/res/;
}
#第三個規則就是通用規則,用來轉發動態請求到后端應用服務器
#非靜態文件請求就默認是動態請求,自己根據實際把握
#畢竟目前的一些框架的流行,帶.php,.jsp后綴的情況很少了
location / {
proxy_pass http://tomcat:8080/
}
1.3 Location解析過程
總結:
1、 先判斷精准命中,如果命中,立即返回結果並結束解析過程。
2、 判斷普通命中,如果有多個命中,“記錄”下來“最長”的命中結果(記錄但不結束,最長的為准)。
3、 繼續判斷正則表達式的解析結果,按配置里的正則表達式順序為准,由上至下開始匹配,一旦匹配成功1個,立即返回結果,並結束解析過程。
4、 普通命中順序無所謂,是因為按命中的長短來確定。正則命中,順序有所謂,因為是從前入往后命中的。
拓展:localtion規則匹配順序
1)匹配順序:先匹配普通location,再匹配正則location【跟配置順序無關】
2)“普通 location ”的匹配規則包含“最大前綴匹配”和“嚴格匹配”,其中“嚴格匹配”包含了普通location嚴格匹配和帶=前綴的嚴格匹配;
嚴格匹配:一,普通location,無任何前綴符號的;二,帶=號前綴符號的嚴格匹配。
“普通location匹配”最終會找到“最大前綴匹配”或者“嚴格匹配”的location,如果找到“嚴格匹配”則不再繼續匹配其他規則;【跟配置順序無關】
例如:http://www.xxxyyy.com:8080/
會嚴格匹配上[ configuration A ]
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 ]
}
http://www.kanronghua.com/documents/ 會嚴格匹配上[ configuration c]
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 ]
}
3)“正則location”匹配規則找到第一個匹配的location,就不在繼續匹配后面的location;【跟配置順序有關】
4)匹配完“普通 location ”后,有的時候需要繼續匹配“正則 location ”,有的時候則不需要繼續匹配“正則 location ”。
兩種情況下,不需要繼續匹配正則 location :
(1) 當普通 location 前面指定了“ ^~ ”,特別告訴 Nginx 本條普通 location 一旦匹配上,則不需要繼續正則匹配;
(2) 當普通location 恰好嚴格匹配上,不是最大前綴匹配,則不再繼續匹配正則。
嚴格匹配:一,普通location,無任何前綴符號的;二,帶=號前綴符號的嚴格匹配。
5)最終生效的location配置:
普通location是“嚴格匹配”或者帶有“ ^~ ”前綴,則直接使用普通location;如果既有普通location的最大前綴匹配,也有正則匹配,則正則匹配覆蓋最大前綴匹配。
例如:
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 ]
}
請求示例,匹配的Location:
- / -> configuration A
- /index.html -> configuration B
- /documents/document.html -> configuration C
- /images/1.gif -> configuration D
- /documents/1.jpg -> configuration E
