温馨提示×

centos apache2如何限流

小樊
47
2026-09-09 00:22:39
栏目: 智能运维

CentOS 上,如果你说的 apache2 实际是指 Apache HTTP Server(httpd)(CentOS 一般用 httpd 而不是 apache2),限流通常有以下几种常见做法。


一、使用 mod_reqtimeout(连接/请求速率限制)

适合防止慢速攻击、限制请求时间。

1. 确认模块已启用

httpd -M | grep reqtimeout

一般默认启用。

2. 配置示例

编辑配置文件(如 /etc/httpd/conf/httpd.conf/etc/httpd/conf.d/limit.conf):

<IfModule mod_reqtimeout.c>
    RequestReadTimeout header=10-20,MinRate=500
    RequestReadTimeout body=10,MinRate=500
</IfModule>

含义:

  • header=10-20:头信息 10 秒内必须接收完,否则最多等 20 秒
  • MinRate=500:最低速率 500 字节/秒

二、使用 mod_evasive(防 CC / 限流利器)

最常用、最推荐

1. 安装

yum install epel-release -y
yum install mod_evasive -y

2. 配置

编辑 /etc/httpd/conf.d/mod_evasive.conf

<IfModule mod_evasive20.c>
    DOSHashTableSize 3097
    DOSPageCount 10
    DOSSiteCount 50
    DOSPageInterval 1
    DOSSiteInterval 1
    DOSBlockingPeriod 60
</IfModule>

含义:

  • 同一 IP 1 秒内访问同一页面 10 次 → 封禁 60 秒
  • 同一 IP 1 秒内总请求 50 次 → 封禁

3. 重启 Apache

systemctl restart httpd

三、使用 mod_limitipconn(限制单 IP 并发连接数)

适合限制下载、刷接口。

1. 安装

yum install mod_limitipconn -y

2. 配置示例

<IfModule mod_limitipconn.c>
    <Location />
        MaxConnPerIP 10
        NoIPLimit image/*
    </Location>
</IfModule>

含义:

  • 单 IP 最多 10 个并发连接

四、使用 iptables / firewalld 限流(系统层)

适合全局限流。

1. 限制单 IP 每秒请求

iptables -A INPUT -p tcp --dport 80 -m limit --limit 20/s --limit-burst 50 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j DROP

2. firewalld 示例

firewall-cmd --add-rich-rule='rule service name="http" limit value="20/s" accept'
firewall-cmd --runtime-to-permanent

五、Nginx 反向代理限流(如果你有 Nginx)

如果你前面有 Nginx:

limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;

server {
    location / {
        limit_req zone=one burst=20 nodelay;
    }
}

推荐组合方案

Web 层mod_evasive + mod_limitipconn
系统层firewalld / iptables
前端Nginx 限流


如果你能告诉我:

  • CentOS 几?
  • Apache 是 httpd 还是真的 apache2?
  • 想限 IP 并发 / 请求频率 / 带宽?

我可以给你精准配置

0