Nginx 的 HTTP 配置主要包括三個(gè)區(qū)塊,結(jié)構(gòu)如下:
http { # 這個(gè)是協(xié)議級(jí)別
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
server { # 這個(gè)是服務(wù)器級(jí)別
listen 80;
server_name localhost;
location / { # 這個(gè)是請(qǐng)求級(jí)別
root html;
index index.html index.htm;
}
}
}
一、location 區(qū)段
- location 是在 server 塊中配置,根據(jù)不同的 URl使用不同的配置,來(lái)處理不同的請(qǐng)求。
- location 是有順序的,會(huì)被第一個(gè)匹配的location 處理。
- 基本語(yǔ)法如下:
location [=|~|~*|^~|@] pattern{……}
二、location 前綴含義
= 表示精確匹配,優(yōu)先級(jí)也是最高的
^~ 表示url以某個(gè)常規(guī)字符串開(kāi)頭,理解為匹配url路徑即可
~ 表示區(qū)分大小寫(xiě)的正則匹配
~* 表示不區(qū)分大小寫(xiě)的正則匹配
!~ 表示區(qū)分大小寫(xiě)不匹配的正則
!~* 表示不區(qū)分大小寫(xiě)不匹配的正則
/ 通用匹配,任何請(qǐng)求都會(huì)匹配到
三、location 配置示例
本地解析host文件
在:C:\Windows\System32\drivers\etc\host
添加:192.168.181.128 www.test.com
1. 沒(méi)有修飾符 表示:必須以指定模式開(kāi)始
[root@localhost html]# pwd
/usr/share/nginx/html
[root@localhost html]# cat abc/2.html
2.html
# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
listen 80;
server_name www.test.com;
location /abc {
root /usr/share/nginx/html;
index 2.html;
}
}
# 那么,如下是對(duì)的:
http://www.test.com/abc
2. = 表示:必須與指定的模式精確匹配
[root@localhost html]# pwd
/usr/share/nginx/html
[root@localhost html]# cat a.html
a
[root@localhost html]# cat b.html
b
# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
listen 80;
server_name www.test.com;
location / {
root /usr/share/nginx/html;
index a.html index.htm;
}
location = / {
root /usr/share/nginx/html;
index b.html index.htm;
}
}
# 那么,如下是對(duì)的:
http://www.test.com/
http://www.test.com/a.html
3. ~ 表示:指定的正則表達(dá)式要區(qū)分大小寫(xiě)
[root@localhost html]# pwd
/usr/share/nginx/html
[root@localhost html]# cat abc/abc.html
abc
[root@localhost html]# cat ABC/ABC.html
ABC
# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
listen 80;
server_name www.test.com;
location ~ /ABC {
root /usr/share/nginx/html;
index ABC.html index.htm;
}
}
# 那么,如下是對(duì)的:
http://www.test.com/ABC
4. ~* 表示:指定的正則表達(dá)式不區(qū)分大小寫(xiě)
[root@localhost html]# pwd
/usr/share/nginx/html
[root@localhost html]# cat abc/1.html
abc
[root@localhost html]# cat ABC/1.html
ABC
# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
listen 80;
server_name www.test.com;
location ~* /abc {
root /usr/share/nginx/html;
index abc.html index.htm;
}
}
# 那么:
訪問(wèn) www.test.com/abc 得到的就是 abc
訪問(wèn) www.test.com/ABC 得到的就是 ABC
5. ^~ :類(lèi)似于無(wú)修飾符的行為,也是以指定模式開(kāi)始,不同的是,如果模式匹配,那么就停止搜索其他模式了。
查找順序和優(yōu)先級(jí)
= 大于 ^~ 大于 ~|~*|!~|!~* 大于 /
多個(gè)location配置的情況下匹配順序?yàn)椋菏紫绕ヅ?=,其次匹配^~, 其次是按正則匹配,最后是交給 / 通用匹配。
當(dāng)有匹配成功時(shí)候,停止匹配,按當(dāng)前匹配規(guī)則處理請(qǐng)求。
================================================
(1) =:表示完全匹配;
(2) ^~:匹配URI的前綴,如果一個(gè)URI同時(shí)滿足兩個(gè)規(guī)則的話,匹配最長(zhǎng)的規(guī)則;
(3) ~:匹配正則表達(dá)式,大小寫(xiě)敏感;
(4) ~*:匹配正則表達(dá)式,大小寫(xiě)不敏感;
優(yōu)先級(jí):(1)> (2) > (3) = (4)
location 區(qū)段匹配示例
location = / {
# 只匹配 / 的查詢(xún).
[ configuration A ]
}
location / {
# 匹配任何以 / 開(kāi)始的查詢(xún),但是正則表達(dá)式與一些較長(zhǎng)的字符串將被首先匹配。
[ configuration B ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 開(kāi)始的查詢(xún)并且停止搜索,不檢查正則表達(dá)式。
[ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配任何以gif, jpg, or jpeg結(jié)尾的文件,但是所有 /images/ 目錄的請(qǐng)求將在Configuration C中處理。
[ configuration D ]
}
各請(qǐng)求的處理如下例:
/ → configuration A
/documents/document.html → configuration B
/images/1.gif → configuration C
/documents/1.jpg → configuration D
四、root 和 alias 指令區(qū)別
location /img {
alias /var/www/image/;
}
#若按照上述配置的話,則訪問(wèn)/img/目錄里面的文件時(shí),ningx會(huì)自動(dòng)去/var/www/image/目錄找文件
location /img {
root /var/www/image;
}
#若按照這種配置的話,則訪問(wèn)/img/目錄下的文件時(shí),nginx會(huì)去/var/www/image/img/目錄下找文件。
- alias 是一個(gè)目錄別名的定義,
- root 則是最上層目錄的定義。