limit-traffic-rate: NGINX 根据给定变量限制流量速率
安装
您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:
- RedHat Enterprise Linux 7、8、9 和 10
- CentOS 7、8、9
- AlmaLinux 8、9
- Rocky Linux 8、9
- Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-limit-traffic-rate
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-limit-traffic-rate
通过在 /etc/nginx/nginx.conf 的顶部添加以下内容来启用模块:
load_module modules/ngx_http_limit_traffic_rate_filter_module.so;
本文档描述了 nginx-module-limit-traffic-rate v1.0.0,于 2024 年 12 月 30 日发布。
注意事项
Nginx 指令 limit_rate 可以限制连接的速度,而 limit_conn 可以根据给定变量限制连接数量。如果客户端是浏览器,它只会打开一个与服务器的连接。速度将限制为 limit_rate,除非客户端是多线程下载工具。
ngx_http_limit_traffic_ratefilter_module 提供了一种方法,通过客户端 IP 或下载 URL 限制总下载速率,即使有多个连接。限制条件可以通过以下指令定义。
要安装,请使用以下 ./configure 选项编译 nginx:
--add-module=path/to/this/directory
limit_traffic_rate 模块需要使用共享内存池。
指令语法与 limit_zone 相同
http {
#limit_traffic_rate_zone rate $request_uri 32m;
limit_traffic_rate_zone rate $remote_addr 32m;
server {
location /download/ {
limit_traffic_rate rate 20k;
}
}
}
GitHub
您可以在 nginx-module-limit-traffic-rate 的 GitHub 仓库 中找到有关此模块的其他配置提示和文档。