nginx 之limit_conn_module和limit_req_module

(1)nginx的请求限制可以通过以下来进行配置:


这俩个模块都可以实现nginx的请求限制,但是它们的实现原理是不一样的。区别就是在连接与请求上。

(2)那么对于Http协议的连接与请求:


Http的连接请求是建立在TCP连接的基础之上的。首先需要有Tcp的三次握手,然后才能有Http的连接请求。然后用客户端和服务器端不断的发送FInAck来保持连接,也就是keepAlive.连接是建立在Http请求的基础之上的。在现在这个时代,其实一次连接可以处理多个的http请求。

(3)各个Http版本的区别:


Http1.0中,TCP是不可以复用的,也就是在客户端在发送了HTTP请求以后,服务器端在返回响应的一定时间内是会断开的。一次连接就对应着一次的请求。在Http1.1版本中,一次连接可以顺序性的对应着对应Http请求。在Http2.0版本中,一次连接可以并行的对应着多次的Http请求。

那么总结来说就是这样:

Http请求建立在一次TCP连接的基础上。一次的TCPa连接至少产生一次的Http请求。也可以产生多次的Http请求。

(4)对于连接限制-limit_conn_zone:

 这个模块的目的主要是对连接进行限制,如果是这样的话,那么我们就需要对连接的状态进行进行存储。那么是用什么来进行存储呢?存储肯定是需要空间的。这个limit_conn_zone就是开辟了这样的一个空间。这个空间里,我们需要对那个作为key要进行说明,比如说,以客户端ip作为Key。那么这样的话,就以http_addr这个变量作为Key。如果要以别的内置变量作为key来作为key来进行配置的时候,那么同样,可以写到key配置的这一项中。那么后面的zone=name:size,就是限制的空间,name指的是空间的名字,size表示的是空间的大小。在真正实现限制的时候就会调用这个空间。在limit_conn zone number中就可以调用这个空间。在limit_conn zone number中,是要结合先定义好的zone 才能使用limit_conn.这个zone指的是我们需要调用的zonename.number指的是并发的限制个数。

(5)对于请求限制-limit_req_zone:

Rate:表示限制请求的速率是多大,一般来说,是以秒为单位的。

(6)具体的配置:


在这里有一个limit_req zone=req_zone;这个req_zone对应的就是开头定义好的zone.Zone=req_zone:1m rate=1r/s;这个表示的是对于同一个客户端过来的,我限制它是11个。$remote_addr和binary_remote_addr,其实处理的都是一个意思。在这里,这个zone的大小设置为了1m的大小。对于1m的空间,存储binary肯定比直接存储remote_addr要小。所以,使用了binary.

(7)具体分析:

现在我们使用压测工具来进行测试,这里的话,使用了ab这个压测工具。

ab -n  50 -c 20 http:///192.168.198.129/1.html

-n 表示的是总的请求数目

-c 表示的是并发请求的数目

请求如下:

[root@localhost backup]# ab -n 40 -c 20 http://192.168.198.129/1.html

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 192.168.198.129 (be patient).....done

 

 

Server Software:        nginx/1.12.2

Server Hostname:        192.168.198.129

Server Port:            80

 

Document Path:          /1.html

Document Length:        128 bytes

 

Concurrency Level:      20

Time taken for tests:   0.016 seconds

Complete requests:      40

Failed requests:        0

Write errors:           0

Total transferred:      14760 bytes

HTML transferred:       5248 bytes

Requests per second:    2563.77 [#/sec] (mean)

Time per request:       7.801 [ms] (mean)

Time per request:       0.390 [ms] (mean, across all concurrent requests)

Transfer rate:          923.86 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    2   2.6      0       7

Processing:     2    2   1.4      2      10

Waiting:        1    2   0.3      2       2

Total:          2    4   2.8      2      12

 

Percentage of the requests served within a certain time (ms)

  50%      2

  66%      3

  75%      3

  80%      9

  90%      9

  95%      9

  98%     12

  99%     12

 100%     12 (longest request)

 

 

 在这里我们可以看到:

Time per request:       7.801 [ms] (mean)  表示的每秒钟可以处理7.801个请求

Time per request:       0.390 [ms] (mean, across all concurrent requests)

并且在这里我们可以看到,所有的请求都已经处理成功了。


 那么如果我们修改服务器端的配置文件为:

limit_conn_zone $binary_remote_addr zone=conn_zone:1m;

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

 server {

    listen       80;

    server_name  localhost;

 

    #charset koi8-r;

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

 

    location /mystatus {

       stub_status;

    }

 

    location / {

        root   /opt/app/code;

         #limit_conn conn_zone 1;

         #limit_req zone=req_zone burst=3 nodelay;

         #limit_req zone=req_zone burst=3;

         limit_req zone=req_zone;

 

limit_req zone=req_zone;这个表示的是对于同一个客户端的请求,我们一秒钟只处理一个。

那么我们使用ab压测工具来尝试一下,配置是否已经生效了。

 [root@localhost backup]# ab -n 40 -c 20 http://192.168.198.129/1.html

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 192.168.198.129 (be patient).....done

 

 

Server Software:        nginx/1.12.2

Server Hostname:        192.168.198.129

Server Port:            80

 

Document Path:          /1.html

Document Length:        128 bytes

 

Concurrency Level:      20

Time taken for tests:   0.017 seconds

Complete requests:      40

Failed requests:        39

   (Connect: 0, Receive: 0, Length: 39, Exceptions: 0)

Write errors:           0

Non-2xx responses:      39

Total transferred:      28869 bytes

HTML transferred:       21071 bytes

Requests per second:    2298.32 [#/sec] (mean)

Time per request:       8.702 [ms] (mean)

Time per request:       0.435 [ms] (mean, across all concurrent requests)

Transfer rate:          1619.88 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    2   1.4      2       4

Processing:     2    5   2.7      4       8

Waiting:        0    5   3.2      3       8

Total:          2    7   1.8      7      10

 

Percentage of the requests served within a certain time (ms)

  50%      7

  66%      8

  75%      8

  80%      9

  90%     10

  95%     10

  98%     10

  99%     10

 100%     10 (longest request)

 

观察压测的结果,我们可以发现,总共有40个请求,其中失败的有39个。

成功处理的只有一个。我们看error.log日志,也可以得出这个结论。

 ######################################################################

 limit_conn conn_zone 1;

表示的是在同一时间内,服务器端只允许一个客户端连接过来。

[root@localhost backup]# ab -n 80 -c 40 http://192.168.198.129/1.html

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 192.168.198.129 (be patient).....done

 

 

Server Software:        nginx/1.12.2

Server Hostname:        192.168.198.129

Server Port:            80

 

Document Path:          /1.html

Document Length:        128 bytes

 

Concurrency Level:      40

Time taken for tests:   0.027 seconds

Complete requests:      80

Failed requests:        0

Write errors:           0

Total transferred:      38880 bytes

HTML transferred:       13824 bytes

Requests per second:    2987.08 [#/sec] (mean)

Time per request:       13.391 [ms] (mean)

Time per request:       0.335 [ms] (mean, across all concurrent requests)

Transfer rate:          1417.70 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    4   2.3      4       8

Processing:     1    4   2.1      5      10

Waiting:        0    3   1.5      4       6

Total:          6    8   1.9      7      11

 

Percentage of the requests served within a certain time (ms)

  50%      7

  66%     10

  75%     10

  80%     10

  90%     10

  95%     11

  98%     11

  99%     11

 100%     11 (longest request)


















  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱coding的同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值