nginx优化限制连接请求limit_req
限制单个IP的请求数量,减少DDOS攻击,节省服务器资源。
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location
Sets the shared memory zone and the maximum burst size of requests. If the requests rate exceeds the rate configured for a zone, their processing is delayed such that requests are processed at a defined rate. Excessive requests are delayed until their number exceeds the maximum burst size in which case the request is terminated with an error. By default, the maximum burst size is equal to zero. For example, the directives
配置如下:
http {
###限制单个IP请求数量 放开指定ip可以没有限制访问
geo $allow_ip { #给ip地址赋予value 区别目标ip和普通ip
default 0;
222.173.94.214 1;
127.0.0.1 1;
}
map $allow_ip $limit_key { #给普通ip赋予新变量
0 $binary_remote_addr;
1 "";
}
limit_req_zone $limit_key zone=req_zone:10m rate=100r/s; #引用这个新变量,设定req——zone的大小 设定请求频率
server {
........
location {
........
limit_req zone=req_zone burst=5 nodelay; #引用这个zone 设置缓冲容器 缓冲容器允许5个 总共是105个请求不延迟处理 nodelay不延时处理
proxy_pass http://c2p;
}
}
}