自己做的网站上传,七星彩网投网站建设,安徽中擎建设公司网站,信誉好的购物网站文章目录 前言一、ngx_http_limit_conn_module二、指令介绍1. limit_conn_zone2.limit_conn3. limit_conn_log_level4. limit_conn_status 案例未限制限制 总结 前言
瞬时大量用户访问服务器#xff0c;导致服务器超载而宕机。 恶意请求攻击服务器#xff0c;导致服务器超载… 文章目录 前言一、ngx_http_limit_conn_module二、指令介绍1. limit_conn_zone2.limit_conn3. limit_conn_log_level4. limit_conn_status 案例未限制限制 总结 前言
瞬时大量用户访问服务器导致服务器超载而宕机。 恶意请求攻击服务器导致服务器超载而宕机。 nginx如何限制每个客户端的并发连接数 一、ngx_http_limit_conn_module
生效阶段 NGX_HTTP_PREACCESS_PHASE 模块默认编译进nginx通过–without-http_limit_conn_module禁用。 生效范围
全部worker进程基于共享内存进入preaccess阶段前不生效限制的有效性取决于key的设计依赖postread阶段的realip模块取到真实的IP。 ngx_http_limit_conn_module官方传送门
二、指令介绍
1. limit_conn_zone
定义共享内存(包含大小)以及key关键字 代码如下示例
Syntax: limit_conn_zone key zonename:size;
Default: —
Context: http2.limit_conn
限制并发连接数 代码如下示例
Syntax: limit_conn zone number;
Default: —
Context: http, server, location3. limit_conn_log_level
限制发生时的日志级别
Syntax: limit_conn_log_level info | notice | warn | error;
Default:
limit_conn_log_level error;
Context: http, server, location
This directive appeared in version 0.8.18.4. limit_conn_status
限制发生时向客户端返回的错误码
Syntax: limit_conn_status code;
Default:
limit_conn_status 503;
Context: http, server, location
This directive appeared in version 1.3.15.案例
未限制
现在limit_conn是注释状态
[roottest20 nginx]# cat conf.d/limit_conn.conf
limit_conn_zone $binary_remote_addr zoneaddr:10m;server {server_name limit_conn.test.io;root html/;error_log /var/log/nginx/myerror.log info; location / {#limit_conn_status 500;#limit_conn_log_level warn;#limit_conn addr 1;}
}
# 如上定义了一个addr的共享区域用$binary_remote_addr作为key。# 用ab 压测
ab -c 1000 -n 100000 http://limit_conn.test.io/limit.html# 查看access.log的内容分析返回状态码全部返回200
[roottest20 nginx]# cat /var/log/nginx/access.log | awk -F {print $9} | sort | uniq -c1 100000 200
限制
[roottest20 nginx]# cat conf.d/limit_conn.conf
limit_conn_zone $binary_remote_addr zoneaddr:10m;server {server_name limit_conn.test.io;root html/;error_log /var/log/nginx/myerror.log info; location / {limit_conn_status 500;limit_conn_log_level warn;limit_conn addr 1;}
}
# 如上定义了一个addr的共享区域用$binary_remote_addr作为key。# 用ab 压测
ab -c 1000 -n 100000 http://limit_conn.test.io/limit.html# 查看access.log的内容分析返回状态码有部分返回了500
[roottest20 nginx]# cat /var/log/nginx/access.log | awk -F {print $9} | sort | uniq -c1 170998 2002676 500 总结
下一节介绍nginx限流