当前位置: 首页 > news >正文

nginx-限制客户端并发数

文章目录

  • 前言
  • 一、ngx_http_limit_conn_module
  • 二、指令介绍
    • 1. limit_conn_zone
    • 2.limit_conn
    • 3. limit_conn_log_level
    • 4. 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 zone=name:size;
Default:	—
Context:	http

2.limit_conn

限制并发连接数
代码如下(示例):

Syntax:	limit_conn zone number;
Default:	—
Context:	http, server, location

3. 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是注释状态

[root@test20 nginx]# cat conf.d/limit_conn.conf 
limit_conn_zone $binary_remote_addr zone=addr: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
[root@test20 nginx]# cat /var/log/nginx/access.log | awk -F' ' '{print $9}' | sort | uniq -c1 100000 200

限制

[root@test20 nginx]# cat conf.d/limit_conn.conf 
limit_conn_zone $binary_remote_addr zone=addr: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
[root@test20 nginx]# cat /var/log/nginx/access.log | awk -F' ' '{print $9}' | sort | uniq -c1 170998 2002676 500

总结

下一节,介绍nginx限流

http://www.lryc.cn/news/393444.html

相关文章:

  • Vatee万腾平台:智能生活的新选择
  • 白嫖A100-interLM大模型部署试用活动,亲测有效-2.Git
  • LeetCode 60.排序排列(dfs暴力)
  • 矩阵分析与应用1-矩阵代数基础
  • Vue的学习之生命周期
  • 【MySQL】表的操作{创建/查看/修改/删除}
  • 基于Python爬虫的城市二手房数据分析可视化
  • 这款新的 AI 语音助手击败了 OpenAI,成为 ChatGPT 最受期待的功能之一
  • CTS单测某个模块和测试项
  • pytorch、pytorch_lightning、torchmetrics版本对应
  • 麒麟系统部署JeecgBoot
  • 要想贵人相助,首先自己得先成为贵人!
  • 使用块的网络 VGG
  • 微信小程序性能与体验优化
  • Android14之获取包名/类名/服务名(二百二十三)
  • FreeU: Free Lunch in Diffusion U-Net——【代码复现】
  • 第三方商城对接重构(HF202407)
  • 如何在Windows 11上复制文件和文件夹路径?这里提供几种方法
  • 大数据Spark 面经
  • 绝区叁--如何在移动设备上本地运行LLM
  • Interview preparation--Https 工作流程
  • 集成学习(三)GBDT 梯度提升树
  • 后端工作之一:CrapApi —— API接口管理系统部署
  • 20240706 xenomai系统中网口(m2/minipcie I210网卡)的实时驱动更换
  • 模型训练之数据集
  • 【TB作品】数码管独立按键密码锁,ATMEGA16单片机,Proteus仿真 atmega16数码管独立按键密码锁
  • 数据库主从复制
  • 昇思25天学习打卡营第13天|BERT
  • 跨平台书签管理器 - Raindrop
  • 均匀采样信号的鲁棒Savistky-Golay滤波(MATLAB)