nginx常用模块(连接限制模块 请求限制模块)

ngx_http_limit_conn_module

连接限制模块

语法

#设置限制的空间
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http

limit_conn_zone         #调用限制模块
key                             #存储的内容
zone=                         #空间
name:                         #空间的名字
size;                           #空间的大小

#调用空间
Syntax: limit_conn zone number;
Default: —
Context: http, server, location

limit_conn                   #调用空间
zone                           #空间名字
number;                      #同一个信息可以保存的次数

[root@web02 /etc/nginx/conf.d]# cat test.conf 
limit_conn_zone $remote_addr zone=conn_zone:1m;
server {
    listen 80;
    server_name www.test.com;
    limit_conn conn_zone 1;
    location / {
        root /code;
        index index.html;
    }
    location /download {
        root /code;
        index index.html;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        allow 10.0.0.1;
        deny all;
        auth_basic on;
        auth_basic_user_file /etc/nginx/auth_basic;
    }
    location /status {
        stub_status;
    }
}

请求限制模块

语法

 配置

[root@web02 /etc/nginx/conf.d]# cat test.conf 
limit_conn_zone $remote_addr zone=conn_zone:1m;
limit_req_zone $remote_addr zone=req_zone:1m rate=1r/s;

server {
    listen 80;
    server_name www.test.com;
    limit_conn conn_zone 1;
    limit_req zone=req_zone;
    location / {
        root /code;
        index index.html;
    }
    location /download {
        root /code;
        index index.html;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        allow all;
        auth_basic on;
        auth_basic_user_file /etc/nginx/auth_basic;
    }
    location /status {
        stub_status;
    }
}

 

[root@web02 /etc/nginx/conf.d]# ab -n 200 -c 2 http://www.test.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.test.com (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:        nginx/1.18.0
Server Hostname:        www.test.com
Server Port:            80

Document Path:          /
Document Length:        13 bytes

Concurrency Level:      2
Time taken for tests:   0.011 seconds
Complete requests:      200
Failed requests:        199
   (Connect: 0, Receive: 0, Length: 199, Exceptions: 0)
Write errors:           0
Non-2xx responses:      199
Total transferred:      76674 bytes
HTML transferred:       39216 bytes
Requests per second:    5492.24 [#/sec] (mean)
Time per request:       0.364 [ms] (mean)
Time per request:       0.182 [ms] (mean, across all concurrent requests)
Transfer rate:          1975.76 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.8      0      12
Processing:     0    0   0.6      0       4
Waiting:        0    0   0.5      0       4
Total:          0    0   1.0      0      12

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

-n         在测试会话中所执行的请求个数

-c          一次产生的请求个数

 

原文地址:https://www.cnblogs.com/chenlifan/p/13561355.html