nginx 模組(8)
獲取全套nginx教程,請訪問 瓦力部落格
小菜簡單介紹nginx模組,模組太多,就不一一介紹。這裡是nginx的所有模組 http://nginx.org/en/docs/ {:target="_blank"}。在後面的操作中用到了 ab
壓力測試工具,不會的夥伴請檢視ab{:target="_blank"}這篇部落格
nginx模組在編譯的時候就作為選項被新增進去,檢視新增哪些模組
nginx -V
1.http_stub_status_module配置
syntax: stub_status; default: - contentx: server,location
下面小菜配置到自己walidream.com的域名下,展示一下做什麼用處的。根據語法知道配置在server,和location下,小菜就配置location下面
cd /etc/nginx/conf.d/ vim server1.conf
將這段程式碼新增到 location /
下面
location /mystatus { stub_status; }
然後在瀏覽器上輸入walidream.com/mystatus

ssl
Active connections: 2#連線數 server accepts handled requests 94 94 86#總握手次數總連線數總請求數 Reading: 0 Writing: 1 Waiting: 1#讀的數寫的數等待的數
2.random_index_module
語法
syntax: random_index on|off; default: random_index off; context: location
隨機的獲取目錄下面字尾為 .html
,進入配置
location / { root /var/share/nginx/html/server; random_index on; }
在/var/share/nginx/html/server目錄下新建1.html,2.html,3.html內容分別為
<!--這個是1.html內容 --> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body style="background:red;"> </body> </html> <!--這個是2.html內容 --> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body style="background:blue;"> </body> </html> <!--這個是3.html內容 --> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body style="background:blank;"> </body> </html>
然後在瀏覽器輸入 walidream.com,多按幾次f5重新整理看看:
3.http_sub_module
替換返回html的內容
1.sub_filter 作用是替換返回html的內容
syntax: sub_filter string replacement; default: - contentx: http,server,location
2.sub_filter_last_modified用來校驗服務端的html內容是否有更新,主要用於快取
syntax: sub_filter_last_modified on|off; default: sub_filter_last_modified off; context: http,server,location
3.sub_filter_once 只匹配第一個還是匹配所有
syntax: sub_filter_once on|off; default: sub_filter_once on; contentx: http,server,location;
4.sub_filter_types 替換的型別
Syntax: sub_filter_types mime-type ...; Default: sub_filter_types text/html; Context: http, server, location
除了 text/html
之外,還在具有指定MIME型別的響應中啟用字串替換。特殊值 *
匹配任何MIME型別。
示例配置
將返回內容中 Golang
字串替換成 Nginx
,只匹配一次。
location / { root /opt/app/code; index sub_module.html sub_module.htm; sub_filter 'Golang' 'Nginx'; sub_filter_once on; }
4.http_limit_conn_module
連線頻率限制
1.limit_conn
Syntax: limit_conn zone number; Default: — Context: http, server, location
2.limit_conn_log_level
Syntax: limit_conn_log_level info | notice | warn | error; Default: limit_conn_log_level error; Context: http, server, location
3.limit_conn_status
Syntax: limit_conn_status code; Default: limit_conn_status 503; Context: http, server, location
4.limit_conn_zone
Syntax: limit_conn_zone key zone=name:size; Default: — Context: http
5.limit_zone
Syntax: limit_zone name $variable size; Default: — Context: http
示例配置
$binary_remote_addr
比 remote_addr
佔用的位元組少。
limit_conn_zone $binary_remote_addr zone=conn_zone:1m; server { listen80; server_name walidream.com; location / { root /opt/app/code; limit_conn conn_zone 1; index sub_module.html sub_module.htm; } }
用壓力測試工具 ab
進行壓力測試。
ab -n 100 -c 20 http://walidream/sub_module.html
5.limit_req_module
請求頻率限制
1.limit_req
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number]; Default: — Context: http, server, location
2.limit_req_log_level
Syntax: limit_req_log_level info | notice | warn | error; Default: limit_req_log_level error; Context: http, server, location
3.limit_req_status
Syntax: limit_req_status code; Default: limit_req_status 503; Context: http, server, location
4.limit_req_zone
Syntax: limit_req_zone key zone=name:size rate=rate [sync]; Default: — Context: http
示例配置
$binary_remote_addr
比 remote_addr
佔用的位元組少。
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s; server { listen80; server_name walidream.com; location / { root /opt/app/code; #limit_req zone=req_zone burst=3 nodelay; #limit_req zone=req_zone burst=3 nodelay; #limit_req zone=req_zone burst=3; limit_req zone=req_zone; index sub_module.html sub_module.htm; } }
用壓力測試工具 ab
進行壓力測試。
ab -n 100 -c 20 http://walidream/sub_module.html
輸出
Server Software:nginx/1.14.1 Server Hostname:walidream.com Server Port:80 Document Path:/sub_module.html Document Length:143 bytes Concurrency Level:20 Time taken for tests:1.009 seconds Complete requests:100 Failed requests:98 (Connect: 0, Receive: 0, Length: 98, Exceptions: 0) Write errors:0 Non-2xx responses:98 Total transferred:72388 bytes HTML transferred:52912 bytes Requests per second:99.06 [#/sec] (mean) Time per request:201.895 [ms] (mean) Time per request:10.095 [ms] (mean, across all concurrent requests) Transfer rate:70.03 [Kbytes/sec] received Connection Times (ms) minmean[+/-sd] medianmax Connect:010 100.001001 Processing:010.112 Waiting:010.212 Total:11299.921001 Percentage of the requests served within a certain time (ms) 50%2 66%2 75%2 80%2 90%2 95%2 98%3 99%1001 100%1001 (longest request)