Skip to content

Rate limiting on Apache with mod_ratelimit, mod_evasive, and friends

Pragmatic rate limiting in Apache — bandwidth caps with mod_ratelimit, abuse mitigation with mod_evasive, and request-per-second limits with mod_qos.

Apache’s rate-limiting story is fragmented across three modules with different scopes. mod_ratelimit caps bandwidth per response. mod_evasive blocks abusive clients with simple hit-count thresholds. mod_qos is the full-featured request-rate limiter — most powerful, also the heaviest to operate. This article is the decision matrix and the configurations we actually deploy for each scenario.

How to verify

Find out which (if any) of the three are loaded and active:

apachectl -M | grep -E 'ratelimit|evasive|qos'
apt list --installed 2>/dev/null | grep -E 'mod-ratelimit|mod-evasive|mod-qos'
ls /etc/apache2/mods-enabled/ | grep -E 'ratelimit|evasive|qos'
sudo apache2ctl -M | grep ratelimit

For an existing install, also check for inline RateLimit directives in vhosts:

grep -rE 'RateLimit|DOSPageCount|QS_LocRequestLimit' /etc/apache2/sites-enabled/

What’s happening

Three modules, three problem shapes:

  • mod_ratelimit — caps the per-response bandwidth. Useful for serving large static files (downloads, video) without saturating uplink. Per-response, not per-client; a single fast client cannot DoS a slow file but can still do many parallel requests.
  • mod_evasive — counts request hits per IP into a small hash table. When a threshold is exceeded (e.g. >10 requests to the same URL in 1 second), the IP is blocked for a configured interval. Cheap, in-process, no shared state across workers (limit is per-child).
  • mod_qos — implements full quality-of-service — concurrent connections per IP, requests per second per location, vhost-wide caps, and reserved capacity for VIP networks. Configurable, persistent across workers via shared memory, and used by sites that need a real layer-7 rate limiter without an edge CDN.

For most setups we operate, the answer is “use the edge CDN for rate limiting at scale, deploy mod_evasive as the local backstop”. Inline mod_ratelimit for downloads. mod_qos only when an edge CDN is not in the picture and the application genuinely needs sophisticated per-location quotas.

The procedure

Bandwidth cap with mod_ratelimit

For serving large files (downloads, video, ISOs) without burning the uplink:

LoadModule ratelimit_module modules/mod_ratelimit.so

<Location /downloads>
    SetOutputFilter RATE_LIMIT
    SetEnv rate-limit 5000     # KiB/s per response
    SetEnv rate-initial-burst 2000
</Location>

rate-limit is in KiB/s. rate-initial-burst is the initial KiB sent at unlimited speed before the cap kicks in — good for small files in the same Location.

Per-IP caps require mod_ratelimit to be combined with RewriteRule + [E=var] to mark requests, or replaced with mod_qos (which has native per-client bandwidth limits).

Abuse mitigation with mod_evasive

Install and minimal config:

sudo apt install -y libapache2-mod-evasive
sudo a2enmod evasive
sudo mkdir -p /var/log/mod_evasive
sudo chown www-data:www-data /var/log/mod_evasive

The packaged default config is conservative. Edit /etc/apache2/mods-enabled/evasive.conf:

<IfModule mod_evasive20.c>
    DOSHashTableSize    3097
    DOSPageCount        10       # max requests/sec for same URL from same IP
    DOSSiteCount        50       # max requests/sec for same site from same IP
    DOSPageInterval     1
    DOSSiteInterval     1
    DOSBlockingPeriod   60       # block for 60 seconds after threshold
    DOSLogDir           /var/log/mod_evasive
    DOSEmailNotify      ops@example.com
    DOSWhitelist        127.0.0.1
    DOSWhitelist        10.0.0.*
</IfModule>

DOSWhitelist exempts internal monitoring and your CI runners that hit the site in bursts. After config changes:

sudo apache2ctl configtest
sudo systemctl reload apache2

The blocked IPs land in the configured log directory as touch-files; a banned IP gets one 403 per request until the block period expires. The block is per-Apache-child — under MPM event with multiple child processes, the limit is effectively higher than the configured number.

Request-rate limits with mod_qos

mod_qos is the most powerful and the most operationally heavy. Install:

sudo apt install -y libapache2-mod-qos
sudo a2enmod qos

A vhost with per-IP and per-location limits:

<IfModule qos_module>
    # Cap concurrent connections per IP
    QS_SrvMaxConnPerIP 50

    # 10 req/s max to /api/* per IP
    <Location /api>
        QS_LocRequestPerSecLimit 10
    </Location>

    # Slow client defense: drop connections that send <1500 bytes/s of request data
    QS_SrvMinDataRate 1500
</IfModule>

After enabling, monitor /server-status?qos (with appropriate ACLs) to see the limiter in action. Tune the thresholds against real traffic — a Black Friday spike on an ecommerce site needs different numbers than a steady B2B SaaS.

Decision matrix

NeedModuleWhy
Cap download bandwidthmod_ratelimitInline, no shared state needed
Block dumb DoS scriptsmod_evasiveCheap, drop-in, per-child limits acceptable
Per-location req/s quotasmod_qosOnly one with proper shared-memory accounting
Per-IP connection capsmod_qosNative QS_SrvMaxConnPerIP
Edge-volume rate limitingCDN (Cloudflare, CloudFront, Fastly)Apache cannot keep up with attacker-scale traffic

Common pitfalls

  • mod_evasive’s hit count is per child process — under high MPM child counts (event MPM with many workers), the effective limit is the configured number times the worker count.
  • mod_qos registers an output filter that can interfere with mod_proxy if both are loaded but not ordered correctly; check the order of LoadModule lines.
  • mod_ratelimit only limits bandwidth on the response body; the connection setup and header send are unthrottled, so it does not defend against slow-loris-style attacks.
  • Whitelisting internal monitoring is mandatory for mod_evasive and mod_qos — without it, the first health-check spike during an incident triggers self-DoS.
  • Behind a CDN or a load balancer, the IP that hits Apache is the upstream’s IP, not the client’s. Load mod_remoteip and configure it to trust X-Forwarded-For from the upstream, or every rate limit will block at the LB rather than the abuser.

Stack Harbor combines edge rate limiting at the CDN with mod_evasive as the on-host backstop, and reaches for mod_qos only when the application genuinely demands per-location quotas. If your Apache box is being scraped flat and there is no inline limiter, that defense is part of the Managed Operations baseline we apply on takeover.