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

nginx-限速-限制并发连接数-限制请求数

nginx-限速

  • 一、为什么要限制用户?
  • 二、限制下载的速度
    • 1. 进入html存放网页的目录,新建一个download的文件夹,用于给用户提供下载
    • 2.修改主配置文件,添加限制下载数据速度的配置
  • 三、限制并发连接数
  • 四、限制请求数


一、为什么要限制用户?

主要是因为服务器的资源有限(cpu、内存、带宽) --》预算有限 --》只能进行控制–》让大家都可以下载或者访问

限速 --》ngx_http_core_module

限制访问并发连接数–》ngx_http_limit_conn_module
爬虫 --》多进程或者多线程

限制请求数 --》ngx_http_limit_req_module


二、限制下载的速度

1. 进入html存放网页的目录,新建一个download的文件夹,用于给用户提供下载

[root@nginx-1 html]# mkdir download

复制/boot目录到download目录下

[root@nginx-1 html]# cd download/
[root@nginx-1 download]# cp /boot . -r
[root@nginx-1 download]# ls
boot

打一个压缩包,提供给用户下载测试使用

[root@nginx-1 download]# tar czf boot.tar.gz boot
[root@nginx-1 download]# ll -h
总用量 179M
dr-xr-xr-x 5 root root 4.0K 8月 15 16:25 boot
-rw-r–r-- 1 root root 179M 8月 15 16:25 boot.tar.gz


2.修改主配置文件,添加限制下载数据速度的配置

[root@nginx-1 download]# cd /usr/local/nginx8/conf/
[root@nginx-1 conf]# vim nginx.confserver {listen       80;server_name  www.feng.com;location / {root   html;index  index.html index.htm;}#添加下面的配置location /download {autoindex on;limit_rate_after 50k;limit_rate       5k;}[root@nginx-1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx8/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx8/conf/nginx.conf test is successful
[root@nginx-1 conf]# nginx -s reload

测试下载的限速的效果

[root@web-2 feng.com]# curl  -O http://192.168.100.157/download/boot.tar.gz% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0  178M    0  276k    0     0   6313      0  8:13:43  0:00:44  8:12:59  5115

三、限制并发连接数

ngx_http_limit_conn_module

limit_conn_zone $binary_remote_addr zone=perip:10m;
定义了一个共享内存区域(zone),用于存储客户端的连接限制状态。

$binary_remote_addr
键(Key):以客户端的二进制格式 IP 地址作为唯一标识。

相比 $remote_addr(文本格式 IP),$binary_remote_addr 更节省内存(固定 4 字节 IPv4 或 16 字节 IPv6)

zone=perip:10m
perip:自定义的共享内存区域名称(可任意命名)
10m:分配 10MB 的共享内存空间,用于存储 IP 和连接数的键值对。

[root@nginx-1 conf]# vim nginx.conf
limit_conn_zone $binary_remote_addr zone=perip:10m;server {listen       80;server_name  www.feng.com;location / {root   html;index  index.html index.htm;}location /download {autoindex on;limit_rate_after 50k;limit_rate       5k;limit_conn perip 1;  #一个客户端只能同时发起一个连接}
[root@nginx-1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx8/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx8/conf/nginx.conf test is successful
[root@nginx-1 conf]# nginx -s reload

测试

503 Service Temporarily Unavailable --》服务临时不可用

[root@web-2 ~]# vim access.sh
#!/bin/bash
for  i in {1..10}
do#启动10个子进程去执行curl程序,去下载文件(curl -O http://192.168.100.157/download/boot.tar.gz)&
done
#等待子进程结束,才退出脚本
wait
[root@web-2 ~]# bash  access.sh [root@nginx-1 logs]# tail -1 access.log 
192.168.100.158 - - [15/Aug/2025:16:53:21 +0800] "GET /download/boot.tar.gz HTTP/1.1" 503 497 "-" "curl/7.76.1"
[root@nginx-1 logs]# tail -1 error.log 
2025/08/15 16:53:21 [error] 4855#4855: *25 limiting connections by zone "perip", client: 192.168.100.158, server: www.feng.com, request: "GET /download/boot.tar.gz HTTP/1.1", host: "192.168.100.157"

四、限制请求数

ngx_http_limit_req_module

建立一个连接,可以发起多次请求,只要在65秒内,不会再次建立连接

[root@nginx-1 conf]# vim nginx.conflimit_conn_zone $binary_remote_addr zone=perip:10m;limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #添加server {listen       80;server_name  www.feng.com;location / {root   html;index  index.html index.htm;limit_req zone=one burst=5; #添加}
[root@nginx-1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx8/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx8/conf/nginx.conf test is successful
[root@nginx-1 conf]# nginx -s reload

测试效果
打开浏览器,按F5 刷新

在这里插入图片描述

在linux使用脚本去测试

[root@web-2 ~]# cat access_url.sh 
#!/bin/bash
for  i in {1..100}
do(curl  http://192.168.100.157)&
done
wait
[root@web-2 ~]# bash  access_url.sh [root@nginx-1 logs]# tail -f access.log 192.168.100.158 - - [15/Aug/2025:17:21:48 +0800] "GET / HTTP/1.1" 503 497 "-" "curl/7.76.1"
192.168.100.158 - - [15/Aug/2025:17:21:48 +0800] "GET / HTTP/1.1" 503 497 "-" "curl/7.76.1"
192.168.100.158 - - [15/Aug/2025:17:21:48 +0800] "GET / HTTP/1.1" 503 497 "-" "curl/7.76.1"
192.168.100.158 - - [15/Aug/2025:17:21:48 +0800] "GET / HTTP/1.1" 503 497 "-" "curl/7.76.1"
192.168.100.158 - - [15/Aug/2025:17:21:48 +0800] "GET / HTTP/1.1" 503 497 "-" "curl/7.76.1"
192.168.100.158 - - [15/Aug/2025:17:21:48 +0800] "GET / HTTP/1.1" 503 497 "-" "curl/7.76.1"
192.168.100.158 - - [15/Aug/2025:17:21:48 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.76.1"
192.168.100.158 - - [15/Aug/2025:17:21:48 +0800] "GET / HTTP/1.1" 503 497 "-" "curl/7.76.1"
http://www.xdnf.cn/news/1342477.html

相关文章:

  • 零音乐基础想创作?通过cpolar,ACE-Step远程编曲如此简单
  • 知识见闻 - 苹果无线键盘A1314说明书
  • 【力扣 Hot100】滑动窗口巧解字串问题
  • 新的 SHAMOS MacOS 窃取程序利用单行终端命令攻击用户
  • 开发者中使用——控制台打印数据
  • Linux mmap内存映射
  • tail -f与less的区别
  • 【系统信息相关】datecal命令
  • 使用 TensorBoardX 实现 PyTorch 神经网络可视化:从入门到进阶
  • 【运维进阶】Shell 变量
  • VASPKIT模版INCAR笔记
  • 同题异构解决leetcode第3646题下一个特殊回文数
  • Effective C++ 条款55:熟悉Boost库
  • 2025-08-21 Python进阶2——数据结构
  • imx6ull-驱动开发篇33——platform 平台驱动模型
  • C++ this 指针
  • 分治思想在系统分流削峰中的实践与Golang前沿实现
  • Python读取和设置PNG图片的像素值
  • MFC随笔—不使用对话框资源模板创建对话框
  • Effective C++ 条款54:熟悉标准库
  • 【lucene】lucene常用查询一览
  • python 项目编号 2025821 有关于中英文数据的收集、处理
  • 数据结构之排序大全(3)
  • Python数据可视化利器:Matplotlib从入门到实战全解析
  • C ++代码学习笔记(一)
  • TDengine IDMP 运维指南(常见问题)
  • 日语学习-日语知识点小记-构建基础-JLPT-N3阶段(18):文法+单词第6回1
  • 虚幻基础:曲线
  • 基于STM32单片机的二维码识别物联网OneNet云仓库系统
  • 图--常见面试问题