本文涉及到的所有組態檔我已經放在了 Nginx 組態檔,大家可以自取,
Nginx 處理一個 HTTP 請求的全程序
前面給大家講了 Nginx 是如何處理 HTTP請求頭部的,接下來就到了真正處理 HTTP 請求的階段了,先看下面這張圖,這張圖是 Nginx 處理 HTTP 請求的示意圖,雖然簡單,但是卻很好的說明了整個程序,
- Read Request Headers:決議請求頭,
- Identify Configuration Block:識別由哪一個 location 進行處理,匹配 URL,
- Apply Rate Limits:判斷是否限速,例如可能這個請求并發的連接數太多超過了限制,或者 QPS 太高,
- Perform Authentication:連接控制,驗證請求,例如可能根據 Referrer 頭部做一些防盜鏈的設定,或者驗證用戶的權限,
- Generate Content:生成回傳給用戶的回應,為了生成這個回應,做反向代理的時候可能會和上游服務(Upstream Services)進行通信,然后這個程序中還可能會有些子請求或者重定向,那么還會走一下這個程序(Internal redirects and subrequests),
- Response Filters:過濾回傳給用戶的回應,比如壓縮回應,或者對圖片進行處理,
- Log:記錄日志,

以上這七個步驟從整體上介紹了一下處理流程,下面還會再說一下實際的處理程序,
Nginx 處理 HTTP 請求的 11 個階段
下面介紹一下詳細的 11 個階段,每個階段都可能對應著一個甚至多個 HTTP 模塊,通過這樣一個模塊對比,我們也能夠很好的理解這些模塊具體是怎么樣發揮作用的,

- POST_READ:在 read 完請求的頭部之后,在沒有對頭部做任何處理之前,想要獲取到一些原始的值,就應該在這個階段進行處理,這里面會涉及到一個 realip 模塊,
- SERVER_REWRITE:和下面的 REWRITE 階段一樣,都只有一個模塊叫 rewrite 模塊,一般沒有第三方模塊會處理這個階段,
- FIND_CONFIG:做 location 的匹配,暫時沒有模塊會用到,
- REWRITE:對 URL 做一些處理,
- POST_WRITE:處于 REWRITE 之后,也是暫時沒有模塊會在這個階段出現,
接下來是確認用戶訪問權限的三個模塊:
- PREACCESS:是在 ACCESS 之前要做一些作業,例如并發連接和 QPS 需要進行限制,涉及到兩個模塊:limt_conn 和 limit_req
- ACCESS:核心要解決的是用戶能不能訪問的問題,例如 auth_basic 是用戶名和密碼,access 是用戶訪問 IP,auth_request 根據第三方服務回傳是否可以去訪問,
- POST_ACCESS:是在 ACCESS 之后會做一些事情,同樣暫時沒有模塊會用到,
最后的三個階段處理回應和日志:
-
PRECONTENT:在處理 CONTENT 之前會做一些事情,例如會把子請求發送給第三方的服務去處理,try_files 模塊也是在這個階段中,
-
CONTENT:這個階段涉及到的模塊就非常多了,例如 index, autoindex, concat 等都是在這個階段生效的,
-
LOG:記錄日志 access_log 模塊,
以上的這些階段都是嚴格按照順序進行處理的,當然,每個階段中各個 HTTP 模塊的處理順序也很重要,如果某個模塊不把請求向下傳遞,后面的模塊是接收不到請求的,而且每個階段中的模塊也不一定所有都要執行一遍,下面就接著講一下各個階段模塊之間的請求順序,
11 個階段的順序處理
如下圖所示,每一個模塊處理之間是有序的,那么這個順序怎么才能得到呢?其實非常簡單,在原始碼 ngx_module.c 中,有一個陣列 ngx_module_name,其中包含了在編譯 Nginx 的時候的 with 指令所包含的所有模塊,它們之間的順序非常關鍵,在陣列中順序是相反的,
char *ngx_module_names[] = {
… …
"ngx_http_static_module",
"ngx_http_autoindex_module",
"ngx_http_index_module",
"ngx_http_random_index_module",
"ngx_http_mirror_module",
"ngx_http_try_files_module",
"ngx_http_auth_request_module",
"ngx_http_auth_basic_module",
"ngx_http_access_module",
"ngx_http_limit_conn_module",
"ngx_http_limit_req_module",
"ngx_http_realip_module",
"ngx_http_referer_module",
"ngx_http_rewrite_module",
"ngx_http_concat_module",
… …
}
灰色部分的模塊是 Nginx 的框架部分去執行處理的,第三方模塊沒有機會在這里得到處理,
在依次向下執行的程序中,也可能不按照這樣的順序,例如,在 access 階段中,有一個指令叫 satisfy,它可以指示當有一個滿足的時候就直接跳到下一個階段進行處理,例如當 access 滿足了,就直接跳到 try_files 模塊進行處理,而不會再執行 auth_basic、auth_request 模塊,
在 content 階段中,當 index 模塊執行了,就不會再執行 auto_index 模塊,而是直接跳到 log 模塊,
整個 11 個階段所涉及到的模塊和先后順序如下圖所示:

下面開始詳細講解一下各個階段,先來看下第一個階段 postread 階段,顧名思義,postread 階段是在正式處理請求之前起作用的,
postread 階段
postread 階段,是 11 個階段的第 1 個階段,這個階段剛付訓取到了請求的頭部,還沒有進行任何處理,我們可以拿到一些原始的資訊,例如,拿到用戶的真實 IP 地址
問題:如何拿到用戶的真實 IP 地址?

我們知道,TCP 連接是由一個四元組構成的,在四元組中,包含了源 IP 地址,而在真實的互聯網中,存在非常多的正向代理和反向代理,例如最終的用戶有自己的內網 IP 地址,運營商會分配一個公網 IP,然后訪問某個網站的時候,這個網站可能使用了 CDN 加速一些靜態檔案或圖片,如果 CDN 沒有命中,那么就會回源,回源的時候可能還要經過一個反向代理,例如阿里云的 SLB,然后才會到達 Nginx,
我們要拿到的地址應該是運營商給用戶分配的公網 IP 地址 115.204.33.1,對這個 IP 來進行并發連接的控制或者限速,而 Nginx 拿到的卻是 2.2.2.2,那么怎么才能拿到真實的用戶 IP 呢?
HTTP 協議中,有兩個頭部可以用來獲取用戶 IP:
- X-Forwardex-For 是用來傳遞 IP 的,這個頭部會把經過的節點 IP 都記錄下來
- X-Real-IP:可以記錄用戶真實的 IP 地址,只能有一個
拿到真實用戶 IP 后如何使用?
針對這個問題,Nginx 是基于變數來使用,
例如 binary_remote_addr、remote_addr 這樣的變數,其值就是真實的 IP,這樣做連接限制也就是 limit_conn 模塊才有意義,這也說明了,limit_conn 模塊只能在 preaccess 階段,而不能在 postread 階段生效,
realip 模塊
-
默認不會編譯進 Nginx
- 需要通過
--with-http_realip_module啟用功能
- 需要通過
-
變數:如果還想要使用原來的 TCP 連接中的地址和埠,需要通過這兩個變數保存
realip_remote_addrrealip_remote_port
-
功能
- 修改客戶端地址
-
指令
-
set_real_ip_from指定可信的地址,只有從該地址建立的連接,獲取的 realip 才是可信的
-
real_ip_header指定從哪個頭部取真實的 IP 地址,默認從
X-Real-IP中取,如果設定從X-Forwarded-For中取,會先從最后一個 IP 開始取 -
real_ip_recursive環回地址,默認關閉,打開的時候,如果
X-Forwarded-For最后一個地址與客戶端地址相同,會過濾掉該地址
-
Syntax: set_real_ip_from address | CIDR | unix:;
Default: —
Context: http, server, location
Syntax: real_ip_header field | X-Real-IP | X-Forwarded-For | proxy_protocol;
Default: real_ip_header X-Real-IP;
Context: http, server, location
Syntax: real_ip_recursive on | off;
Default: real_ip_recursive off;
Context: http, server, location
實戰
上面關于 real_ip_recursive 指令可能不太容易理解,我們來實戰練習一下,先來看 real_ip_recursive 默認關閉的情況:
- 重新編譯一個帶有 realip 模塊的 nginx
關于如何編譯 Nginx,詳見:https://iziyang.github.io/2020/03/10/1-nginx/
# 下載 nginx 原始碼,在原始碼目錄下執行
./configure --prefix=自己指定的目錄 --with-http_realip_module
make
make install
- 然后去上一步中自己指定的 Nginx 安裝目錄
#屏蔽默認的 nginx.conf 檔案的 server 塊內容,并添加一行
include /Users/mtdp/myproject/nginx/test_nginx/conf/example/*.conf;
# 在 example 目錄下建立 realip.conf,set_real_ip_from 可以設定為自己的本機 IP
server {
listen 80;
server_name ziyang.realip.com;
error_log /Users/mtdp/myproject/nginx/nginx/logs/myerror.log debug;
set_real_ip_from 192.168.0.108;
#real_ip_header X-Real-IP;
real_ip_recursive off;
# real_ip_recursive on;
real_ip_header X-Forwarded-For;
location / {
return 200 "Client real ip: $remote_addr\n";
}
}
在上面的組態檔中,我設定了可信代理地址為本機地址,real_ip_recursive 為默認的 off,real_ip_header 設為從 X-Forwarded-For 中取,
- 多載組態檔
./sbin/nginx -s reload
- 測驗回應結果
? test_nginx curl -H 'X-Forwarded-For: 1.1.1.1,192.168.0.108' ziyang.realip.com
Client real ip: 192.168.0.108
然后再來測驗 real_ip_recursive 打開的情況:
- 組態檔中打開
real_ip_recursive
server {
listen 80;
server_name ziyang.realip.com;
error_log /Users/mtdp/myproject/nginx/nginx/logs/myerror.log debug;
set_real_ip_from 192.168.0.108;
#real_ip_header X-Real-IP;
#real_ip_recursive off;
real_ip_recursive on;
real_ip_header X-Forwarded-For;
location / {
return 200 "Client real ip: $remote_addr\n";
}
}
- 測驗回應結果
? test_nginx curl -H 'X-Forwarded-For: 1.1.1.1,2.2.2.2,192.168.0.108' ziyang.realip.com
Client real ip: 2.2.2.2
所以這里面也可看出來,如果使用 X-Forwarded-For 獲取 realip 的話,需要打開 real_ip_recursive,并且,realip 依賴于 set_real_ip_from 設定的可信地址,
那么有人可能就會問了,那直接用 X-Real-IP 來選取真實的 IP 地址不就好了,這是可以的,但是 X-Real-IP 是 Nginx 獨有的,不是 RFC 規范,如果客戶端與服務器之間還有其他非 Nginx 軟體實作的代理,就會造成取不到 X-Real-IP 頭部,所以這個要根據實際情況來定,
rewrite 階段的 rewrite 模塊
下面來看一下 rewrite 模塊,
首先 rewrite 階段分為兩個,一個是 server_rewrite 階段,一個是 rewrite,這兩個階段都涉及到一個 rewrite 模塊,而在 rewrite 模塊中,有一個 return 指令,遇到該指令就不會再向下執行,直接回傳回應,


return 指令
return 指令的語法如下:
- 回傳狀態碼,后面跟上 body
- 回傳狀態碼,后面跟上 URL
- 直接回傳 URL
Syntax: return code [text];
return code URL;
return URL;
Default: —
Context: server, location, if
回傳狀態碼包括以下幾種:
- Nginx 自定義
- 444:立刻關閉連接,用戶收不到回應
- HTTP 1.0 標準
- 301:永久重定向
- 302:臨時重定向,禁止被快取
- HTTP 1.1 標準
- 303:臨時重定向,允許改變方法,禁止被快取
- 307:臨時重定向,不允許改變方法,禁止被快取
- 308:永久重定向,不允許改變方法
return 指令與 error_page
error_page 的作用大家肯定經常見到,當訪問一個網站出現 404 的時候,一般不會直接出現一個 404 NOT FOUND,而是會有一個比較友好的頁面,這就是 error_page 的功能,
Syntax: error_page code ... [=[response]] uri;
Default: —
Context: http, server, location, if in location
我們來看幾個例子:
1. error_page 404 /404.html;
2. error_page 500 502 503 504 /50x.html;
3. error_page 404 =200 /empty.gif;
4. error_page 404 = /404.php;
5. location / {
error_page 404 = @fallback;
}
location @fallback {
proxy_pass http://backend;
}
6. error_page 403 http://example.com/forbidden.html;
7. error_page 404 =301 http://example.com/notfound.html;
那么現在就會有兩個問題,大家看下下面這個配置檔案:
server {
server_name ziyang.return.com;
listen 80;
root html/;
error_page 404 /403.html;
#return 405;
location / {
#return 404 "find nothing!";
}
}
- 當 server 下包含 error_page 且 location 下有 return 指令的時候,會執行哪一個呢?
- return 指令同時出現在 server 塊下和同時出現在 location 塊下,它們有合并關系嗎?
這兩個問題我們通過實戰驗證一下,
實戰
- 將上面的配置添加到組態檔 return.conf
- 在本機的 hosts 檔案中系結 ziyang.return.com 為本地的 IP 地址
- 訪問一個不存在的頁面
? test_nginx curl ziyang.return.com/text
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
這個時候可以看到,是 error_page 生效了,回傳的回應是 403,
那么假如打開了 location 下 return 指令的注釋呢?
- 打開
return指令注釋,reload 組態檔 - 重新訪問頁面
? test_nginx curl ziyang.return.com/text
find nothing!%
這時候,return 指令得到了執行,也就是第一個問題,當 server 下包含 error_page 且 location 下有 return 指令的時候,會執行 return 指令,
下面再看一下 server 下的 return 指令和 location 下的 return 指令會執行哪一個,
- 打開
server下return指令的注釋,reload 組態檔 - 重新訪問頁面
? test_nginx curl ziyang.return.com/text
<html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
針對上面兩個問題也就有了答案:
-
當
server下包含error_page且location下有return指令的時候,會執行哪一個呢?會執行
location下的return指令, -
return指令同時出現在server塊下和同時出現在location塊下,它們有合并關系嗎?沒有合并關系,先遇到哪個
return指令就先執行哪一個,
rewrite 指令
rewrite 指令用于修改用戶傳入 Nginx 的 URL,來看下 rewrite 的指令規則:
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
它的功能主要有下面幾點:
- 將
regex指定的 URL 替換成replacement這個新的 URL- 可以使用正則運算式及變數提取
- 當
replacement以 http:// 或者 https:// 或者 $schema 開頭,則直接回傳 302 重定向 - 替換后的 URL 根據 flag 指定的方式進行處理
- last:用
replacement這個 URL 進行新的 location 匹配 - break:break 指令停止當前腳本指令的執行,等價于獨立的 break 指令
- redirect:回傳 302 重定向
- permanent:回傳 301 重定向
- last:用
指令示例
現在我們有這樣的一個目錄結構:
html/first/
└── 1.txt
html/second/
└── 2.txt
html/third/
└── 3.txt
組態檔如下所示:
server {
listen 80;
server_name rewrite.ziyang.com;
rewrite_log on;
error_log logs/rewrite_error.log notice;
root html/;
location /first {
rewrite /first(.*) /second$1 last;
return 200 'first!\n';
}
location /second {
rewrite /second(.*) /third$1;
return 200 'second!\n';
}
location /third {
return 200 'third!\n';
}
location /redirect1 {
rewrite /redirect1(.*) $1 permanent;
}
location /redirect2 {
rewrite /redirect2(.*) $1 redirect;
}
location /redirect3 {
rewrite /redirect3(.*) http://rewrite.ziyang.com$1;
}
location /redirect4 {
rewrite /redirect4(.*) http://rewrite.ziyang.com$1 permanent;
}
}
那么我們的問題是:
- return 指令 與 rewrite 指令的順序關系?
- 訪問 /first/3.txt,/second/3.txt,/third/3.txt 分別回傳的是什么?
- 如果不攜帶 flag 會怎么樣?
帶著這三個問題,我們來實際演示一下,
實戰
準備作業
- 將上面的配置添加到組態檔 rewrite.conf
- 在本機的 hosts 檔案中系結 rewrite.ziyang.com 為 127.0.0.1
last flag
首先訪問 rewrite.ziyang.com/first/3.txt,結果如下:
? ~ curl rewrite.ziyang.com/first/3.txt
second!
為什么結果是 second! 呢?應該是 third! 呀,可能有人會有這樣的疑問,實際的匹配步驟如下:
- curl rewrite.ziyang.com/first/3.txt
- 由于
rewrite /first(.*) /second$1 last;這條指令的存在,last 表示使用新的 URL 進行 location 匹配,因此接下來會去匹配 second/3.txt - 匹配到 /second 塊之后,會依次執行指令,最后回傳 200
- 注意,location 塊中雖然也改寫了 URL,但是并不會去繼續匹配,因為后面沒有指定 flag,
break flag
下面將 rewrite /second(.*) /third$1; 這條指令加上 break flag,rewrite /second(.*) /third$1 break;
繼續訪問 rewrite.ziyang.com/first/3.txt,結果如下:
? ~ curl rewrite.ziyang.com/first/3.txt
test3%
這時候回傳的是 3.txt 檔案的內容 test3,實際的匹配步驟如下:
- curl rewrite.ziyang.com/first/3.txt
- 由于
rewrite /first(.*) /second$1 last;這條指令的存在,last 表示使用新的 URL 進行 location 匹配,因此接下來會去匹配 second/3.txt - 匹配到 /second 塊之后,由于 break flag 的存在,會繼續匹配 rewrite 過后的 URL
- 匹配 /third location
因此,這個程序實際請求的 URL 是 rewrite.ziyang.com/third/3.txt,這樣自然結果就是 test3 了,你還可以試試訪問 rewrite.ziyang.com/third/2.txt 看看會回傳什么,
redirect 和 permanent flag
組態檔中還有 4 個 location,你可以分別試著訪問一下,結果是這樣的:
- redirect1:回傳 301
- redirect2:回傳 302
- redirect3:回傳 302
- redirect4:回傳 301
rewrite 行為記錄日志
主要是一個指令 rewrite_log:
Syntax: rewrite_log on | off;
Default: rewrite_log off;
Context: http, server, location, if
這個指令打開之后,會把 rewrite 的日志寫入 logs/rewrite_error.log 日志檔案中,這是請求 /first/3.txt 的日志記錄:
2020/05/06 06:24:05 [notice] 86959#0: *25 "/first(.*)" matches "/first/3.txt", client: 127.0.0.1, server: rewrite.ziyang.com, request: "GET /first/3.txt HTTP/1.1", host: "rewrite.ziyang.com"
2020/05/06 06:24:05 [notice] 86959#0: *25 rewritten data: "/second/3.txt", args: "", client: 127.0.0.1, server: rewrite.ziyang.com, request: "GET /first/3.txt HTTP/1.1", host: "rewrite.ziyang.com"
2020/05/06 06:24:05 [notice] 86959#0: *25 "/second(.*)" matches "/second/3.txt", client: 127.0.0.1, server: rewrite.ziyang.com, request: "GET /first/3.txt HTTP/1.1", host: "rewrite.ziyang.com"
2020/05/06 06:24:05 [notice] 86959#0: *25 rewritten data: "/third/3.txt", args: "", client: 127.0.0.1, server: rewrite.ziyang.com, request: "GET /first/3.txt HTTP/1.1", host: "rewrite.ziyang.com"
if 指令
if 指令也是在 rewrite 階段生效的,它的語法如下所示:
Syntax: if (condition) { ... }
Default: —
Context: server, location
它的規則是:
- 條件 condition 為真,則執行大括號內的指令;同時還遵循值指令的繼承規則(詳見我之前的文章 Nginx 的配置指令)
那么 if 指令的條件運算式包含哪些內容呢?它的規則如下:
- 檢查變數為慷訓者值是否為 0
- 將變數與字串做匹配,使用 = 或 !=
- 將變數與正則運算式做匹配
- 大小寫敏感,~ 或者 !~
- 大小寫不敏感,~* 或者 !~*
- 檢查檔案是否存在,使用 -f 或者 !-f
- 檢查目錄是否存在,使用 -d 或者 !-d
- 檢查檔案、目錄、軟鏈接是否存在,使用 -e 或者 !-e
- 檢查是否為可執行檔案,使用 -x 或者 !-x
下面是一些例子:
if ($http_user_agent ~ MSIE) { # 與變數 http_user_agent 匹配
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") { # 與變數 http_cookie 匹配
set $id $1;
}
if ($request_method = POST) { # 與變數 request_method 匹配,獲取請求方法
return 405;
}
if ($slow) { # slow 變數在 map 模塊中自定義,也可以進行匹配
limit_rate 10k;
}
if ($invalid_referer) {
return 403;
}
find_config 階段
當經過 rewrite 模塊,匹配到 URL 之后,就會進入 find_config 階段,開始尋找 URL 對應的 location 配置,
location 指令
指令語法
還是老規矩,咱們先來看一下 location 指令的語法:
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
Syntax: merge_slashes on | off;
Default: merge_slashes on;
Context: http, server
這里面有一個 merge_slashes 指令,這個指令的作用是,加入 URL 中有兩個重復的 /,那么會合并為一個,這個指令默認是打開的,只有當對 URL 進行 base64 之類的編碼時才需要關閉,
匹配規則
location 的匹配規則是僅匹配 URI,忽略引數,有下面三種大的情況:
- 前綴字串
- 常規匹配
- =:精確匹配
- ^~:匹配上后則不再進行正則運算式匹配
- 正則運算式
- ~:大小寫敏感的正則匹配
- ~*:大小寫不敏感
- 用戶內部跳轉的命名 location
- @
對于這些規則剛看上去肯定是很懵的,完全不知道在說什么,下面來實戰看幾個例子,
實戰
先看一下 Nginx 的組態檔:
server {
listen 80;
server_name location.ziyang.com;
error_log logs/error.log debug;
#root html/;
default_type text/plain;
merge_slashes off;
location ~ /Test1/$ {
return 200 'first regular expressions match!\n';
}
location ~* /Test1/(\w+)$ {
return 200 'longest regular expressions match!\n';
}
location ^~ /Test1/ {
return 200 'stop regular expressions match!\n';
}
location /Test1/Test2 {
return 200 'longest prefix string match!\n';
}
location /Test1 {
return 200 'prefix string match!\n';
}
location = /Test1 {
return 200 'exact match!\n';
}
}
問題就來了,訪問下面幾個 URL 會分別回傳什么內容呢?
/Test1
/Test1/
/Test1/Test2
/Test1/Test2/
/test1/Test2
例如訪問 /Test1 時,會有幾個部分都匹配上:
- 常規前綴匹配:location /Test1
- 精確匹配:location = /Test1
訪問 /Test1/ 時,也會有幾個部分匹配上:
- location ~ /Test1/$
- location ^~ /Test1/
那么究竟會匹配哪一個呢?Nginx 其實是遵循一套規則的,如下圖所示:

全部的前綴字串是放置在一棵二叉樹中的,Nginx 會分為兩部分進行匹配:
- 先遍歷所有的前綴字串,選取最長的一個前綴字串,如果這個字串是 = 的精確匹配或 ^~ 的前綴匹配,會直接使用
- 如果第一步中沒有匹配上 = 或 ^~,那么會先記住最長匹配的前綴字串 location
- 按照 nginx.conf 檔案中的配置依次匹配正則運算式
- 如果所有的正則運算式都沒有匹配上,那么會使用最長匹配的前綴字串
下面看下實際的回應是怎么樣的:
? test_nginx curl location.ziyang.com/Test1
exact match!
? test_nginx curl location.ziyang.com/Test1/
stop regular expressions match!
? test_nginx curl location.ziyang.com/Test1/Test2
longest regular expressions match!
? test_nginx curl location.ziyang.com/Test1/Test2/
longest prefix string match!
? test_nginx curl location.ziyang.com/Test1/Test3
stop regular expressions match!
- /Test1 匹配 location = /Test1
- /Test1/ 匹配 location ^~ /Test1/
- /Test1/Test2 匹配 location ~* /Test1/(\w+)$
- /Test1/Test2/ 匹配 location /Test1/Test2
- /Test1/Test3 匹配 location ^~ /Test1/
這里面重點解釋一下 /Test1/Test3 的匹配程序:
- 遍歷所有可以匹配上的前綴字串,總共有兩個
- ^~ /Test1/
- /Test1
- 選取最長的前綴字串 /Test1/,由于前面有 ^~ 禁止正則運算式匹配,因此直接使用 location ^~ /Test1/ 的規則
- 回傳
stop regular expressions match!
preaccess 階段
下面就來到了 preaccess 階段,我們經常會遇到一個問題,就是如何限制每個客戶端的并發連接數?如何限制訪問頻率?這些就是在 preaccess 階段處理完成的,顧名思義,preaccess 就是在連接之前,先來看下 limit_conn 模塊,

limit_conn 模塊
這里面涉及到的模塊是 ngx_http_limit_conn_module,它的基本特性如下:
- 生效階段:
NGX_HTTP_PREACCESS_PHASE階段 - 模塊:
http_limit_conn_module - 默認編譯進 Nginx,通過
--without-http_limit_conn_module禁用 - 生效范圍
- 全部 worker 行程(基于共享記憶體)
- 進入 preaccess 階段前不生效
- 限制的有效性取決于 key 的設計:依賴 postread 階段的 realip 模塊取到真實 IP
這里面有一點需要注意,就是 limit_conn key 的設計,所謂的 key 指的就是對哪個變數進行限制,通常我們取的都是用戶的真實 IP,
說完了 limit_conn 的模塊,再來說一下指令語法,
指令語法
- 定義共享記憶體(包括大小),以及 key 關鍵字
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
- 限制并發連接數
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
- 限制發生時的日志級別
Syntax: limit_conn_log_level info | notice | warn | error;
Default: limit_conn_log_level error;
Context: http, server, location
- 限制發生時向客戶端回傳的錯誤碼
Syntax: limit_conn_status code;
Default: limit_conn_status 503;
Context: http, server, location
實戰
下面又到了實戰的環節了,通過一個實際的例子來看一下以上的幾個指令是怎么起作用的,
老規矩,先上組態檔:
limit_conn_zone $binary_remote_addr zone=addr:10m;
#limit_req_zone $binary_remote_addr zone=one:10m rate=2r/m;
server {
listen 80;
server_name limit.ziyang.com;
root html/;
error_log logs/myerror.log info;
location /{
limit_conn_status 500;
limit_conn_log_level warn;
limit_rate 50;
limit_conn addr 1;
#limit_req zone=one burst=3 nodelay;
#limit_req zone=one;
}
}
- 在本地的 hosts 檔案中添加 limit.ziyang.com 為本機 IP
在這個組態檔中,做了兩條限制,一個是 limit_rate 限制為 50 個位元組,并發連接數 limit_conn 限制為 1,
? test_nginx curl limit.ziyang.com
這時候訪問 limit.ziyang.com 這個站點,會發現速度非常慢,因為每秒鐘只有 50 個位元組,
如果再同時訪問這個站點的話,則會回傳 500,
我在另一個終端里面同時訪問:
? ~ curl limit.ziyang.com
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
可以看到,Nginx 直接回傳了 500,
limit_req 模塊
在本節開頭我們就提出了兩個問題:
-
如何限制每個客戶端的并發連接數?
-
如何限制訪問頻率?
第一個問題限制并發連接數的問題已經解決了,下面來看第二個問題,
這里面生效的模塊是 ngx_http_limit_req_module,它的基本特性如下:
- 生效階段:
NGX_HTTP_PREACCESS_PHASE階段 - 模塊:
http_limit_req_module - 默認編譯進 Nginx,通過
--without-http_limit_req_module禁用 - 生效演算法:leaky bucket 演算法
- 生效范圍
- 全部 worker 行程(基于共享記憶體)
- 進入 preaccess 階段前不生效
leaky bucket 演算法
leaky bucket 叫漏桶演算法,其他用來限制請求速率的還有令牌環演算法等,這里面不展開講,
漏桶演算法的原理是,先定義一個桶的大小,所有進入桶內的請求都會以恒定的速率被處理,如果請求太多超出了桶的容量,那么就會立刻回傳錯誤,用一張圖解釋一下,

這張圖里面,水龍頭在不停地滴水,就像用戶發來的請求,所有的水滴都會以恒定的速率流出去,也就是被處理,漏桶演算法對于突發流量有很好的限制作用,會將所有的請求平滑的處理掉,
指令語法
- 定義共享記憶體(包括大小),以及 key 關鍵字和限制速率
Syntax: limit_req_zone key zone=name:size rate=rate ;
Default: —
Context: http
rate 單位為 r/s 或者 r/m(每分鐘或者每秒處理多少個請求)
- 限制并發連接數
Syntax: limit_req zone=name [burst=number] [nodelay];
Default: —
Context: http, server, location
- burst 默認為 0
- nodelay,如果設定了這個引數,那么對于漏桶中的請求也會立刻回傳錯誤
- 限制發生時的日志級別
Syntax: limit_req_log_level info | notice | warn | error;
Default: limit_req_log_level error;
Context: http, server, location
- 限制發生時向客戶端回傳的錯誤碼
Syntax: limit_req_status code;
Default: limit_req_status 503;
Context: http, server, location
實戰
在實際驗證之前呢,需要注意兩個問題:
- limit_req 與 limit_conn 配置同時生效時,哪個優先級高?
- nodelay 添加與否,有什么不同?
添加組態檔,這個組態檔與上一節的組態檔其實是相同的只不過需要注釋一下:
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=2r/m;
server {
listen 80;
server_name limit.ziyang.com;
root html/;
error_log logs/myerror.log info;
location /{
limit_conn_status 500;
limit_conn_log_level warn;
#limit_rate 50;
#limit_conn addr 1;
#limit_req zone=one burst=3 nodelay;
limit_req zone=one;
}
}
結論:在 limit_req zone=one 指令下,超出每分鐘處理的請求數后就會立刻回傳 503,
? test_nginx curl limit.ziyang.com
<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body>
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
改變一下注釋的指令:
limit_req zone=one burst=3;
#limit_req zone=one;
在沒有添加 burst 引數時,會立刻回傳錯誤,而加上之后,不會回傳錯誤,而是等待請求限制解除,直到可以處理請求時再回傳,
再來看一下 nodelay 引數:
limit_req zone=one burst=3 nodelay;
添加了 nodelay 之后,請求在沒有達到 burst 限制之前都可以立刻被處理并回傳,超出了 burst 限制之后,才會回傳 503,
現在可以回答一下剛開始提出的兩個問題:
- limit_req 與 limit_conn 配置同時生效時,哪個優先級高?
- limit_req 在 limit_conn 處理之前,因此是 limit_req 會生效
- nodelay 添加與否,有什么不同?
- 不添加 nodelay,請求會等待,直到能夠處理請求;添加 nodelay,在不超出 burst 的限制的情況下會立刻處理并回傳,超出限制則會回傳 503,
access 階段
經過 preaccess 階段對用戶的限流之后,就到了 access 階段,
access 模塊
這里面涉及到的模塊是 ngx_http_access_module,它的基本特性如下:
- 生效階段:
NGX_HTTP_ACCESS_PHASE階段 - 模塊:
http_access_module - 默認編譯進 Nginx,通過
--without-http_access_module禁用 - 生效范圍
- 進入 access 階段前不生效
指令語法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
access 模塊提供了兩條指令 allow 和 deny,來看幾個例子:
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
對于用戶訪問來說,這些指令是順序執行的,當滿足了一條之后,就不會再向下執行,這個模塊比較簡單,我們這里不做實戰演練了,
auth_basic 模塊
auth_basic 模塊是用作用戶認證的,當開啟了這個模塊之后,我們通過瀏覽器訪問網站時,就會回傳一個 401 Unauthorized,當然這個 401 用戶不會看見,瀏覽器會彈出一個對話框要求輸入用戶名和密碼,這個模塊使用的是 RFC2617 中的定義,
指令語法
- 基于 HTTP Basic Authutication 協議進行用戶密碼的認證
- 默認編譯進 Nginx
- --without-http_auth_basic_module
- disable ngx_http_auth_basic_module
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
這里面我們會用到一個工具叫 htpasswd,這個工具可以用來生成密碼檔案,而 auth_basic_user_file 就依賴這個密碼檔案,
htpasswd 依賴安裝包 httpd-tools
生成密碼的命令為:
htpasswd –c file –b user pass
生成的密碼檔案的格式為:
# comment
name1:password1
name2:password2:comment
name3:password3
實戰
- 在 example 目錄下生成密碼檔案 auth.pass
htpasswd -bc auth.pass ziyang 123456
- 添加組態檔
server {
server_name access.ziyang.com;
listen 80;
error_log logs/error.log debug;
default_type text/plain;
location /auth_basic {
satisfy any;
auth_basic "test auth_basic";
auth_basic_user_file example/auth.pass;
deny all;
}
}
- 多載 Nginx 組態檔
- 在 /etc/hosts 檔案中添加 access.ziyang.com
這時候訪問 access.ziyang.com 就會彈出對話框,提示輸入密碼:

auth_request 模塊
- 功能:向上游的服務轉發請求,若上游服務回傳的回應碼是 2xx,則繼續執行,若上游服務回傳的回應碼是 2xx,則繼續執行,若上游服務回傳的是 401 或者 403,則將回應回傳給客戶端
- 原理:收到請求后,生成子請求,通過反向代理技術把請求傳遞給上游服務
- 默認未編譯進 Nginx,需要通過 --with-http_auth_request_module 編譯進去
指令語法
Syntax: auth_request uri | off;
Default: auth_request off;
Context: http, server, location
Syntax: auth_request_set $variable value;
Default: —
Context: http, server, location
實戰
- 在上一個組態檔中添加以下內容
server {
server_name access.ziyang.com;
listen 80;
error_log logs/error.log debug;
#root html/;
default_type text/plain;
location /auth_basic {
satisfy any;
auth_basic "test auth_basic";
auth_basic_user_file example/auth.pass;
deny all;
}
location / {
auth_request /test_auth;
}
location = /test_auth {
proxy_pass http://127.0.0.1:8090/auth_upstream;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
}
- 這個組態檔中,/ 路徑下會將請求轉發到另外一個服務中去,可以用 nginx 再搭建一個服務
- 如果這個服務回傳 2xx,那么鑒權成功,如果回傳 401 或 403 則鑒權失敗
限制所有 access 階段模塊的 satisfy 指令
指令語法
Syntax: satisfy all | any;
Default: satisfy all;
Context: http, server, location
satisfy 指令有兩個值一個是 all,一個是 any,這個模塊對 acces 階段的三個模塊都生效:
- access 模塊
- auth_basic 模塊
- auth_request 模塊
- 其他模塊
如果 satisfy 指令的值是 all 的話,就表示必須所有 access 階段的模塊都要執行,都通過了才會放行;值是 any 的話,表示有任意一個模塊得到執行即可,
下面有幾個問題可以加深一下理解:
-
如果有 return 指令,access 階段會生效嗎?
return 指令屬于 rewrite 階段,在 access 階段之前,因此不會生效,
-
多個 access 模塊的順序有影響嗎?
ngx_http_auth_request_module, ngx_http_auth_basic_module, ngx_http_access_module,有影響
-
輸對密碼,下面可以訪問到檔案嗎?
location /{ satisfy any; auth_basic "test auth_basic"; auth_basic_user_file examples/auth.pass; deny all; }可以訪問到,因為
satisfy的值是 any,因此只要有模塊滿足,即可放行, -
如果把 deny all 提到 auth_basic 之前呢?
依然可以,因為各個模塊執行順序和指令的順序無關,
-
如果改為 allow all,有機會輸入密碼嗎?
沒有機會,因為 allow all 是 access 模塊,先于 auth_basic 模塊執行,
precontent 階段
講到了這里,我們再來回顧一下 Nginx 處理 HTTP 請求的 11 個階段:

現在我們已經來到了 precontent 階段,這個階段只有 try_files 這一個指令,
try_files 模塊
指令語法
Syntax: try_files file ... uri;
try_files file ... =code;
Default: —
Context: server, location
- 模塊:
ngx_http_try_files_module模塊 - 依次試圖訪問多個 URL 對應的檔案(由 root 或者 alias 指令指定),當檔案存在時,直接回傳檔案內容,如果所有檔案都不存在,則按照最后一個 URL 結果或者 code 回傳
實戰
下面我們實際看一個例子:
server {
server_name tryfiles.ziyang.com;
listen 80;
error_log logs/myerror.log info;
root html/;
default_type text/plain;
location /first {
try_files /system/maintenance.html
$uri $uri/index.html $uri.html
@lasturl;
}
location @lasturl {
return 200 'lasturl!\n';
}
location /second {
try_files $uri $uri/index.html $uri.html =404;
}
}
結果如下:
- 訪問 /first 實際上到了 lasturl,然后回傳 200
- 訪問 /second 則回傳了 404
這兩個結果都與組態檔是一致的,
? test_nginx curl tryfiles.ziyang.com/second
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
? test_nginx curl tryfiles.ziyang.com/first
lasturl!
mirror 模塊
mirror 模塊可以實時拷貝流量,這對于需要同時訪問多個環境的請求是非常有用的,
指令語法
- 模塊:
ngx_http_mirror_module模塊,默認編譯進 Nginx- 通過 --without-http_mirror_module 移除模塊
- 功能:處理請求時,生成子請求訪問其他服務,對子請求的回傳值不做處理
Syntax: mirror uri | off;
Default: mirror off;
Context: http, server, location
Syntax: mirror_request_body on | off;
Default: mirror_request_body on;
Context: http, server, location
實戰
- 組態檔如下所示,需要再開啟另外一個 Nginx 來接收請求
server {
server_name mirror.ziyang.com;
listen 8001;
error_log logs/error_log debug;
location / {
mirror /mirror;
mirror_request_body off;
}
location = /mirror {
internal;
proxy_pass http://127.0.0.1:10020$request_uri;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
}
- 在 access.log 檔案中可以看到有請求記錄日志
content 階段
下面開始就到了 content 階段,先來看 content 階段的 static 模塊,雖然這是位于 content 階段的最后一個處理模塊,但是這里先來介紹它,

static 模塊
root 和 alias 指令
先來一下 root 和 alias 這兩個指令,這兩個指令都是用來映射檔案路徑的,
Syntax: alias path;
Default: —
Context: location
Syntax: root path;
Default: root html;
Context: http, server, location, if in location
- 功能:將 URL 映射為檔案路徑,以回傳靜態檔案內容
- 差別:root 會將完整 URL 映射進檔案路徑中,alias 只會將 location 后的 URL 映射到檔案路徑
實戰
下面來看一個問題:
現在有一個檔案路徑:
html/first/
└── 1.txt
組態檔如下所示:
server {
server_name static.ziyang.com;
listen 80;
error_log logs/myerror.log info;
location /root {
root html;
}
location /alias {
alias html;
}
location ~ /root/(\w+\.txt) {
root html/first/$1;
}
location ~ /alias/(\w+\.txt) {
alias html/first/$1;
}
location /RealPath/ {
alias html/realpath/;
return 200 '$request_filename:$document_root:$realpath_root\n';
}
}
那么訪問以下 URL 會得到什么回應呢?
/root
/alias
/root/1.txt
/alias/1.txt
? test_nginx curl static.ziyang.com/alias/1.txt
test1%
? test_nginx curl static.ziyang.com/alias/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
? test_nginx curl static.ziyang.com/root/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
? test_nginx curl static.ziyang.com/root/1.txt
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>
訪問這四個路徑分別得到的結果是:
- /root:404
- /alias:200
- /root/1.txt:404
- /alias/1.txt:200
這是為什么呢?是因為,root 在映射 URL 時,會把 location 中的路徑也加進去,也就是:
static.ziyang.com/root/實際訪問的是html/root/static.ziyang.com/root/1.txt實際是html/first/1.txt/root/1.txtstatic.ziyang.com/alias/實際上是正確訪問到了html檔案夾,由于后面有/的存在,因此實際訪問的是html/index.htmlstatic.ziyang.com/alias/1.txt實際訪問的是html/first/1.txt,檔案存在
三個相關變數
還是上面的組態檔:
location /RealPath/ {
alias html/realpath/;
return 200 '$request_filename:$document_root:$realpath_root\n';
}
這里有一個問題,在訪問 /RealPath/1.txt 時,這三個變數的值各為多少?
為了解答這個問題,我們先來解釋三個變數:
- request_filename:待訪問檔案的完整路徑
- document_root:由 URI 和 root/alias 指令生成的檔案夾路徑(可能包含軟鏈接的路徑)
- realpath_root:將 document_root 中的軟鏈接替換成真實路徑
為了驗證這三個變數,在 html 目錄下建立一個軟鏈接指向 first 檔案夾:
ln -s first realpath
? html curl static.ziyang.com/realpath/1.txt
/Users/mtdp/myproject/nginx/test_nginx/html/realpath/1.txt:/Users/mtdp/myproject/nginx/test_nginx/html/realpath/:/Users/mtdp/myproject/nginx/test_nginx/html/first
可以看出來,三個路徑分別是:
- /Users/mtdp/myproject/nginx/test_nginx/html/realpath/1.txt
- /Users/mtdp/myproject/nginx/test_nginx/html/realpath/
- /Users/mtdp/myproject/nginx/test_nginx/html/first
還有其他的一些配置指令,例如:
靜態檔案回傳時的 Content-Type
Syntax: types { ... }
Default: types { text/html html; image/gif gif; image/jpeg jpg; }
Context: http, server, location
Syntax: default_type mime-type;
Default: default_type text/plain;
Context: http, server, location
Syntax: types_hash_bucket_size size;
Default: types_hash_bucket_size 64;
Context: http, server, location
Syntax: types_hash_max_size size;
Default: types_hash_max_size 1024;
Context: http, server, location
未找到檔案時的錯誤日志
Syntax: log_not_found on | off;
Default: log_not_found on;
Context: http, server, location
在生產環境中,經常可能會有找不到檔案的情況,錯誤日志中就會列印出來:
[error] 10156#0: *10723 open() "/html/first/2.txt/root/2.txt" failed (2: No such file or directory)
如果不想記錄日志,可以關掉,
重定向跳轉的域名
現在有另外一個問題,當我們訪問目錄時最后沒有帶 /,static 模塊會回傳 301 重定向,那么這個規則是怎么定義的呢,看下面三個指令:
# 該指令決定重定向時的域名,可以決定回傳哪個域名
Syntax: server_name_in_redirect on | off;
Default: server_name_in_redirect off;
Context: http, server, location
# 該指令決定重定向時的埠
Syntax: port_in_redirect on | off;
Default: port_in_redirect on;
Context: http, server, location
# 該指令決定是否填域名,默認是打開的,也就是回傳絕對路徑
Syntax: absolute_redirect on | off;
Default: absolute_redirect on;
Context: http, server, location
這三個指令的實際用法來實戰演示一下,先來看組態檔:
server {
server_name return.ziyang.com dir.ziyang.com;
server_name_in_redirect on;
listen 8088;
port_in_redirect on;
absolute_redirect off;
root html/;
}
absolute_redirect 默認是打開的,我們把它關閉了,看下是怎么回傳的:
? test_nginx curl localhost:8088/first -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.8
Date: Tue, 12 May 2020 00:31:36 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: /first/
這個時候看到回傳的頭部 Location 中沒有加上域名,
下面再把 absolute_redirect 打開(默認是打開的,因此注釋掉就行了),看下回傳什么:
absolute_redirect onserver_name_in_redirect onport_in_redirect on
? test_nginx curl localhost:8088/first -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.8
Date: Tue, 12 May 2020 00:35:49 GMT
Content-Type: text/html
Content-Length: 169
Location: http://return.ziyang.com:8088/first/
Connection: keep-alive
可以看到,這時候就回傳了域名,而且回傳的是我們配置的主域名加埠號,這是因為,server_name_in_redirect 和 port_in_redirect 這兩個指令打開了,如果關閉掉這兩個指令,看下回傳什么:
-
absolute_redirect on -
server_name_in_redirect off -
port_in_redirect off
? test_nginx curl localhost:8088/first -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.8
Date: Tue, 12 May 2020 00:39:31 GMT
Content-Type: text/html
Content-Length: 169
Location: http://localhost/first/
Connection: keep-alive
這兩個指令都設定為 off 之后,會發現回傳的不再是主域名加埠號,而是我們請求的域名和埠號,如果在請求頭中加上 Host,那么就會用 Host 請求頭中的域名,
index 模塊
-
模塊:
ngx_http_index_module -
功能:指定
/結尾的目錄訪問時,回傳 index 檔案內容 -
語法:
Syntax: index file ...; Default: index index.html; Context: http, server, location -
先于 autoindex 模塊執行
這個模塊,當我們訪問以 / 結尾的目錄時,會去找 root 或 alias 指令的檔案夾下的 index.html,如果有這個檔案,就會把檔案內容回傳,也可以指定其他檔案,
autoindex 模塊
-
模塊:
ngx_http_autoindex_module,默認編譯進 Nginx,使用--without-http_autoindex_module取消 -
功能:當 URL 以
/結尾時,嘗試以 html/xml/json/jsonp 等格式回傳 root/alias 中指向目錄的目錄結構 -
語法:
# 開啟或關閉 Syntax: autoindex on | off; Default: autoindex off; Context: http, server, location # 當以 HTML 格式輸出時,控制是否轉換為 KB/MB/GB Syntax: autoindex_exact_size on | off; Default: autoindex_exact_size on; Context: http, server, location # 控制以哪種格式輸出 Syntax: autoindex_format html | xml | json | jsonp; Default: autoindex_format html; Context: http, server, location # 控制是否以本地時間格式顯示還是 UTC 格式 Syntax: autoindex_localtime on | off; Default: autoindex_localtime off; Context: http, server, location
實戰
- 組態檔如下:
server {
server_name autoindex.ziyang.com;
listen 8080;
location / {
alias html/;
autoindex on;
#index b.html;
autoindex_exact_size on;
autoindex_format html;
autoindex_localtime on;
}
}
這里我把 index b.html 這條指令給注釋掉了,而 index 模塊是默認編譯進 Nginx 的,且默認指令是 index index.html,因此,會去找是否有 index.html 這個檔案,
- 打開瀏覽器,訪問 autoindex.ziyang.com:8080,html 目錄下默認是有 index.html 檔案的,因此顯示結果為:

- 打開
index b.html指令注釋,由于 html 檔案夾下并不存在 b.html 這個檔案,所以請求會走到 autoindex 模塊,顯示目錄:

后面的檔案大小顯示格式就是由 autoindex_exact_size on; 這條指令決定的,
concat模塊
下面介紹一個可以提升小檔案性能的模塊,這個模塊是由阿里巴巴開發的,在淘寶網中有廣泛應用,
-
模塊:ngx_http_concat_module
-
模塊開發者:Tengine(https://github.com/alibaba/nginx-http-concat) --add-module=../nginx-http-concat/
-
功能:合并多個小檔案請求,可以明顯提升 HTTP 請求的性能
-
指令:
#在 URI 后面加上 ??,通過 ”,“ 分割檔案,如果還有引數,則在最后通過 ? 添加引數 concat on | off default concat off Context http, server, location concat_types MIME types Default concat_types: text/css application/x-javascript Context http, server, location concat_unique on | off Default concat_unique on Context http, server, location concat_max_files numberp Default concat_max_files 10 Context http, server, location concat_delimiter string Default NONE Context http, server, locatione concat_ignore_file_error on | off Default off Context http, server, location
打開淘寶主頁,會發現小檔案都是通過這個模塊來提高性能的:

這里就不做實戰了,感興趣的同學可以自己去編譯一下這個模塊,做一下實驗,我把組態檔放在這里:
server {
server_name concat.ziyang.com;
error_log logs/myerror.log debug;
concat on;
root html;
location /concat {
concat_max_files 20;
concat_types text/plain;
concat_unique on;
concat_delimiter ':::';
concat_ignore_file_error on;
}
}
log 階段
下面終于來到了 11 個階段的最后一個階段,記錄請求訪問日志的 log 模塊,
- 功能:將 HTTP 請求相關資訊記錄到日志
- 模塊:
ngx_http_log_module,無法禁用
access 日志格式
Syntax: log_format name [escape=default|json|none] string ...;
Default: log_format combined "...";
Context: http
默認的 combined 日志格式:
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent ' '"$http_referer"
"$http_user_agent"';
配置日志檔案路徑
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
-
path 路徑可以包含變數:不打開 cache 時每記錄一條日志都需要打開、關閉日志檔案
-
if 通過變數值控制請求日志是否記錄
-
日志快取
-
功能:批量將記憶體中的日志寫入磁盤
-
寫入磁盤的條件:
所有待寫入磁盤的日志大小超出快取大小;
達到 flush 指定的過期時間;
worker 行程執行 reopen 命令,或者正在關閉,
-
-
日志壓縮
- 功能:批量壓縮記憶體中的日志,再寫入磁盤
- buffer 大小默認為 64KB
- 壓縮級別默認為 1(1最快壓縮率最低,9最慢壓縮率最高)
- 打開日志壓縮時,默認打開日志快取功能
對日志檔案名包含變數時的優化
Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
Default: open_log_file_cache off;
Context: http, server, location
- max:快取內的最大檔案句柄數,超出后用 LRU 演算法淘汰
- inactive:檔案訪問完后在這段時間內不會被關閉,默認 10 秒
- min_uses:在 inactive 時間內使用次數超過 min_uses 才會繼續存在記憶體中,默認 1
- valid:超出 valid 時間后,將對快取的日志檔案檢查是否存在,默認 60 秒
- off:關閉快取功能
日志模塊沒有實戰,
到了這里,我們已經將 Nginx 處理 HTTP 請求的 11 個階段全部梳理了一遍,每個階段基本都有對應的模塊,相信對于這樣一個全流程的決議,大家都能夠看懂 Nginx 的配置了,在此之上,還能夠按照需求靈活配置出自己想要的配置,這樣就真正的掌握了 11 個階段,
最后,歡迎大家關注我的個人博客:iziyang.github.io
本文首發于我的個人博客:iziyang.github.io
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/77496.html
標籤:Linux
下一篇:wget簡單使用(1)
