nginx 模塊(8)

獲取全套nginx教程,請(qǐng)?jiān)L問瓦力博客

小菜簡(jiǎn)單介紹nginx模塊,模塊太多,就不一一介紹。這里是nginx的所有模塊http://nginx.org/en/docs/{:target="_blank"}。在后面的操作中用到了ab壓力測(cè)試工具,不會(huì)的伙伴請(qǐng)查看ab{:target="_blank"}這篇博客

nginx模塊在編譯的時(shí)候就作為選項(xiàng)被添加進(jìn)去,查看添加哪些模塊

nginx -V

1.http_stub_status_module配置

syntax: stub_status;
default: -
contentx: server,location

下面小菜配置到自己walidream.com的域名下,展示一下做什么用處的。根據(jù)語法知道配置在server,和location下,小菜就配置location下面

cd /etc/nginx/conf.d/

vim server1.conf

將這段代碼添加到 location /下面

location /mystatus {
    stub_status;
}   

然后在瀏覽器上輸入walidream.com/mystatus

ssl
Active connections: 2  #連接數(shù)
server accepts handled requests
 94 94 86  #總握手次數(shù)  總連接數(shù)  總請(qǐng)求數(shù)
Reading: 0 Writing: 1 Waiting: 1  #讀的數(shù)   寫的數(shù)  等待的數(shù)

2.random_index_module

語法

syntax: random_index on|off;
default: random_index off;
context: location

隨機(jī)的獲取目錄下面后綴為.html,進(jìn)入配置

location / {
    root /var/share/nginx/html/server;
    random_index on;
}

在/var/share/nginx/html/server目錄下新建1.html,2.html,3.html內(nèi)容分別為

<!--這個(gè)是1.html內(nèi)容 -->
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body style="background:red;">
    
</body>
</html>

<!--這個(gè)是2.html內(nèi)容 -->

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body style="background:blue;">
    
</body>
</html>

<!--這個(gè)是3.html內(nèi)容 -->

<!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的內(nèi)容

1.sub_filter 作用是替換返回html的內(nèi)容

syntax: sub_filter string replacement;  
default: -
contentx: http,server,location

2.sub_filter_last_modified用來校驗(yàn)服務(wù)端的html內(nèi)容是否有更新,主要用于緩存

syntax: sub_filter_last_modified on|off;
default: sub_filter_last_modified off;
context: http,server,location

3.sub_filter_once 只匹配第一個(gè)還是匹配所有

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類型的響應(yīng)中啟用字符串替換。特殊值*匹配任何MIME類型。

示例配置

將返回內(nèi)容中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_addrremote_addr占用的字節(jié)少。

limit_conn_zone $binary_remote_addr zone=conn_zone:1m;

server {
    listen      80;
    server_name walidream.com;
    
    location / {
        root /opt/app/code;
        limit_conn conn_zone 1;
        index sub_module.html sub_module.htm;
    }
}

用壓力測(cè)試工具ab進(jìn)行壓力測(cè)試。

ab -n 100 -c 20 http://walidream/sub_module.html

5.limit_req_module

請(qǐng)求頻率限制

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_addrremote_addr占用的字節(jié)少。

limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;

server {
    listen      80;
    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;
    }
}

用壓力測(cè)試工具ab進(jìn)行壓力測(cè)試。

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)
              min  mean[+/-sd] median   max
Connect:        0   10 100.0      0    1001
Processing:     0    1   0.1      1       2
Waiting:        0    1   0.2      1       2
Total:          1   12  99.9      2    1001

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)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容