Large downloads share an Apache server's outbound bandwidth. Apache mod_ratelimit caps each download response's transfer speed; parallel downloads can still multiply total bandwidth. For limits on requests per IP, see HTTP request-rate limiting with HAProxy.

Apache provides this control through mod_ratelimit. The module adds an output filter named RATE_LIMIT, then reads the rate-limit environment variable to throttle each HTTP response in KiB/s while Apache sends the body to the client. An optional rate-initial-burst value allows the first part of the response to leave at full speed before the cap takes over.

Commands below use the current Debian and Ubuntu layout with /etc/apache2, a2enmod, and the apache2 service.

Steps to limit Apache download speed with mod_ratelimit:

  1. Open a terminal with sudo privileges.
  2. Enable the mod_ratelimit module.
    $ sudo a2enmod ratelimit
    Considering dependency env for ratelimit:
    Module env already enabled
    Enabling module ratelimit.
    To activate the new configuration, you need to run:
      systemctl restart apache2

    Red Hat family systems usually load the module through the httpd module configuration under /etc/httpd/conf.modules.d/ instead of using a2enmod.

  3. Open the Apache virtual host file that serves the large downloads.
    $ sudo vi /etc/apache2/sites-available/example.com.conf

    Use the matching TLS virtual host file when the download URL is served over HTTPS.

    Each throttled download occupies an Apache worker for the entire transfer, including with the event MPM. Concurrent slow downloads can exhaust available workers and prevent other requests from being served.

  4. Add a Location block that applies the RATE_LIMIT filter only to the download path.
    <Location "/downloads">
            SetOutputFilter RATE_LIMIT
            SetEnv rate-limit 256
    </Location>

    rate-limit is measured in KiB/s, so the example limits each matching response to about 256 KiB/s. Add SetEnv rate-initial-burst 512 inside the same block when the first 512 KiB should leave at full speed before throttling starts.

    Placing the filter at VirtualHost scope throttles every response for that site, including HTML, CSS, JavaScript, and images. Keep the filter inside a narrow Location or Directory block unless the whole site really needs the cap.

  5. Save the configuration file.
  6. Test the Apache configuration for syntax errors.
    $ sudo apache2ctl configtest
    Syntax OK
  7. Restart the apache2 service to load the module and updated virtual host configuration.
    $ sudo systemctl restart apache2

    If the restart fails or the service does not answer after the change, inspect sudo journalctl --unit=apache2 --no-pager --lines=20 before retrying.

  8. Download a file of a few megabytes from the limited path.
    $ curl -o /dev/null http://example.com/downloads/test.bin
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 2048k  100 2048k    0     0   247k      0  0:00:08  0:00:08 --:--:--  234k

    The Average Dload column should finish close to 256k. When a rate-initial-burst is set, use a file larger than the burst so the cap shows.

Share this guide