nginx限速模塊 limit_conn不生效

Nginx: limit_conn不生效
0
Nginx版本: 1.16.1

default.conf

    # 添加規則
    limit_conn_zone $binary_remote_addr zone=perip:10m;
server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        # 應用規則
        limit_conn perip 1;
    }
    ...
}

2. 問題
使用ab命令進行壓力測試,發現限制最大連接數無效

# 命令
ab -n 50 -c 10 http://192.168.1.183/index.html

# 執行結果
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.183 (be patient).....done


Server Software:        nginx/1.20.0
Server Hostname:        192.168.1.183
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      10
Time taken for tests:   0.007 seconds
Complete requests:      50
Failed requests:        0
Total transferred:      42250 bytes
HTML transferred:       30600 bytes
Requests per second:    7039.28 [#/sec] (mean)
Time per request:       1.421 [ms] (mean)
Time per request:       0.142 [ms] (mean, across all concurrent requests)
Transfer rate:          5808.78 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:     0    1   0.2      1       1
Waiting:        0    1   0.1      1       1
Total:          1    1   0.2      1       2

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      2
  95%      2
  98%      2
  99%      2
 100%      2 (longest request)


3. 原因
默認的Nginx的index.html太小並且處於內網情況下,在測試過程中不能做到真正的併發,請求完成速度太快了,將測試網頁替換成一個大文件。

4. 解決辦法
不算是解決辦法,因爲配置沒有任何問題。此處只是模擬現實請求,測試Nignx限制最大連接數。

4.1. 生成大文件
# 切換到網頁路徑
cd /usr/share/nginx/html

# 生成一個200m大文件,名字爲test
dd if=/dev/zero of=test bs=1M count=20

4.2. 壓力測試工具測試
# 測試命令
ab -n 50 -c 10 http://192.168.1.183/test

# 結果
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.183 (be patient).....done


Server Software:        nginx/1.20.0
Server Hostname:        192.168.1.183
Server Port:            80

Document Path:          /test
Document Length:        494 bytes

Concurrency Level:      10
Time taken for tests:   0.388 seconds
Complete requests:      50
Failed requests:        1
   (Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Non-2xx responses:      49
Total transferred:      209749170 bytes
HTML transferred:       209739406 bytes
Requests per second:    128.92 [#/sec] (mean)
Time per request:       77.566 [ms] (mean)
Time per request:       7.757 [ms] (mean, across all concurrent requests)
Transfer rate:          528153.32 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:    10   24  53.5     13     388
Waiting:        1   16  10.8     12      41
Total:         11   25  53.5     13     388

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     13
  75%     13
  80%     32
  90%     42
  95%     42
  98%    388
  99%    388
 100%    388 (longest request)

4.3. 完成
當前Non-2xx responses: 49,表示有49個請求不是2xx狀態碼,被Nginx服務器拒絕。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章