Transferred from: Http://siwei.me/blog/posts/nginx-ip
Reference: http://tengine.taobao.org/document_cn/http_limit_req_cn.html
Httplimitreqmodul limit the number of instances of the same IP access over a period of time
http{...#定义一个名为allips的limit_req_zone用来存储session, the size is 10M of memory, #以 $binary _remote_addr as key, limiting the average request per second to 20, #1M能存储16000个状态, The value of Rete must be an integer, #如果限制两秒钟一个请求, can be set to 30r/m limit_req_zone $binary _remote_addr zone=allips:10m rate= 20r/s ... server{... location {... #限制每ip每秒不超过20个请求, the number of leaky barrels burst 5 #brust的意思就是, if the 1th second, 2,3,4 seconds request is 19, #第5秒的请求为25个是被允许的. #但是如果你第1秒就25个请求, the request for 2 seconds exceeds 20 returns a 503 error. #nodelay, if this option is not set, the average rate limit request is strictly used, #第1秒25个请求时, 5 requests are placed in the first 2 seconds of execution, Span class= "hljs-comment" > #设置nodelay, 25 requests will be executed in the first 1 seconds. limit_req zone=allips burst=5 nodelay; ...} ...}
(Test does not pass) Httplimitzonemodule Limit number of concurrent connections instances Limit_zone can only be defined in the HTTP scope, Limit_conn may be defined at the HTTP server location scope
http{... #定义一个名为one的limit_zone, size 10M memory to store session, #以 $binary _remote_addr to key after #nginx 1.18 replaced the Limit_conn with Limit_conn_zone #且只能放在http作用域 limit_conn_zone one 500k; ... } ...}
Allows up to 10 connections per IP, 1MB per connection
Inside Your server block configuration:
10m;
Inside your location block configuration:
10;
In this example, it would allow connections per IP with 1 Mbit each.
If you only limit the speed of a req:
Limit_rate 50k;
Nginx Limited Access speed