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)
The download limit will be applied per download connection, restricting each download to a specific maximum speed.
Add the loading of mod_ratelimit into Apache’s configuration.
<IfVersion >= 2.4>
LoadModule ratelimit_module modules/mod_ratelimit.so
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”.
SetEnv rate-limit 350
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 -
* This works best under IE, as Chrome will not download the same file multiple times at the same time.
More Specific Limits
*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.