Download Speed Limit and Throttling for Apache 2.4

Restricting download speeds is extremely easy with Apache 2.4, which now comes standard with mod_ratelimit (a very simple and straight-forward module).

Download limits can be set per:

  • Server (all websites)
  • VirtualHost (specific website)
  • URL
  • The download limit will be applied per download connection, restricting each download to a specific maximum speed.

    Load mod_ratelimit

    Add the loading of mod_ratelimit into Apache’s configuration.

    <IfVersion >= 2.4>
        LoadModule ratelimit_module modules/mod_ratelimit.so
    </IfVersion>

    Limit Download Speed

    To limit download speed per URL in a specific website, open the website’s VirtualHost files (both the HTTP and SSL VH), and insert the rate limiting configuration inside the VirtualHost block.

    This example sets the limit to 350KB/s for all downloads under URL “/downloads”.

    <IfModule mod_ratelimit.c>
        <Location /downloads>
            SetOutputFilter RATE_LIMIT
            SetEnv rate-limit 350
        </Location>
    </IfModule>

    Save file(s), and restart Apache for configuration changes to take effect.

    Test Bandwidth Throttling

    Place a large file into the /downloads location, and attempt to download this file multiple times. Check the results -

    apache-download-limits

    * This works best under IE, as Chrome will not download the same file multiple times at the same time.

    More Specific Limits

    Anything other then per connection limits require more comprehensive Apache modules such as mod_bw (docs) or mod_limitipconn (docs).

    *mod_security can also limit bandwidth, but I would not recommend it as it’s too complex to setup, use, and maintain.

    Also, according to the mod_ratelimit docs (well, the comments in the docs), you can dynamically adjust this rate limit per request, using PHP…

    Note that if you are using PHP, you can set rate-limit to 0 in httpd.conf (no limiting) and then dynamically set “rate-limit” env variable for each request in your PHP scripts, but you cannot use putenv() function for this. You must use apache_setenv() in order to make it work properly.

    This could allow you to create your own management script that tracks the use of, and allots, the download bandwidth.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>