Wiki:
Http://wiki.nginx.org/HttpLimitReqModule
Leaky bucket principle (leaky bucket):
Http://en.wikipedia.org/wiki/Leaky_bucket
Instance:
#以用户二进制IP地址, the definition of three leaky barrels, drop rate 1-3req/sec, bucket space 1m,1m can maintain about 16,000 (IP) status Limit_req_zone $binary _remote_addr zone=qps1:1m
1r/s;
Limit_req_zone $binary _remote_addr zone=qps2:1m rate=2r/s;
Limit_req_zone $binary _remote_addr zone=qps3:1m rate=3r/s; server {#速率qps = 1, peak burst=5, deferred request #严格按照漏桶速率qps = 1 Process Concurrent requests that are less than #在峰值burst = 5 of requests per second, are suspended, and delay processing #超出请求数限制则直接返回503 #客户端只要控制并发在峰值 [ Burst] inside, will not trigger Limit_req_error_log # example 1: initiates a concurrent request = 6, rejects 1, handles 1, enters the deferred queue 4: #time request refuse sucess delay #00:01 6 1 1 4 #00: a 0 0 1 3 #00:0 0 1 2 #00:0 0 1 1 #00:05 0 0 1
0 Location/delay {limit_req zone=qps1 burst=5;} #速率qps = 1, peak burst=5, no delay request #加了nodelay之后, leaky bucket control a length of average QPS = leaky bucket rate, allow instantaneous peak QPS > Leaky barrel QPS #所以峰值时的最高qps = (brust+qps-1) =5 # The request will not be delay, either processed, or returned directly to the 503 #客户端需要控制qps每秒请求数 and will not trigger Limit_req_error_log # example 2: A concurrent request is initiated every 5 seconds to peak, due to the average Qps=1 in time period So still meet the leaky bucket rate: #time request refuse sucess #00:5 0 5 #00:5 0 5 #00:10 5 0 5 # Example 3: Consecutive concurrent requests per second = 5, due to the average qps>>1 within the time period, the exceeded request was rejected: #time request refuse sucess #00:5 0 5 #00:02 5
4 1 #00:5 4 1 Location/nodelay {limit_req zone=qps1 burst=5 nodelay;}
}