126、高并发下的限流配置(nginx使用令牌桶)

本篇来继续讲解Nginx高并发下的优化策略,

可以配置的参数

主文件配置参数

worker_processes 2; #调整到与CPU数量一致

events {
worker_connection 65535; #每个worker最大并发连接数
}

 

下面设置nginx的令牌桶进行限流的功能

http模块下面设置请求的进入速度,下图是每秒5个请求进入(每秒分发5个令牌)

   limit_req_zone $binary_remote_addr zone=req_one:10m rate=5r/s;

 

在server下面的location模块中,设置令牌桶的大小为50,nodelay表示超过令牌数量部分请求,直接返回503

   server {
        listen       8081;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://linuxidc;
            
            # 一个地址最多可以存在10个链接
            limit_req zone=req_one burst=50 nodelay;  # 每个地址每秒只能请求一次,burst=120 一共有120块令牌,并且每秒钟只新增1块令牌,120块令牌发完后,多出来的请求就会返回503
      
        }
}

 

 

进行并发测试

由下面结果可以看出,并发数量为5个,一共55个请求,其中有4个请求因为没有获取到令牌,

被nginx直接返回503失败了。

[root@centos-1 conf]# ab -c 5 -n 55 http://101.37.26.152:8081/pafc-demo/nlp/home
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 101.37.26.152 (be patient).....done


Server Software:        nginx/1.13.11
Server Hostname:        101.37.26.152
Server Port:            8081

Document Path:          /pafc-demo/nlp/home
Document Length:        5396 bytes

Concurrency Level:      5
Time taken for tests:   0.074 seconds
Complete requests:      55
Failed requests:        4
   (Connect: 0, Receive: 0, Length: 4, Exceptions: 0)
Write errors:           0
Non-2xx responses:      4
Total transferred:      286284 bytes
HTML transferred:       277344 bytes
Requests per second:    740.01 [#/sec] (mean)
Time per request:       6.757 [ms] (mean)
Time per request:       1.351 [ms] (mean, across all concurrent requests)
Transfer rate:          3761.61 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2    3   0.1      3       3
Processing:     3    3   0.3      3       4
Waiting:        3    3   0.3      3       4
Total:          5    6   0.3      6       7

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值