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;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具