If your website has downloadable files, then it might be a good idea to limit bandwidth per connection in Apache, to save on data and server costs. It is very easy to restrict download speeds in Apache using the mod_ratelimit module that comes pre-installed with Apache 2.4. You can restrict download speed per:
- Server (across all websites)
- Virtual Host
How to Limit Bandwidth per Connection in Apache
Here are the steps to limit bandwidth per connection in Apache. The download limit is applied per connection limiting each download to a maximum speed.
1. Enable mod_ratelimit
Open Apache config file in a text editor
Ubuntu / Linux Mint
$ sudo vim /etc/apache2/apache2.conf
$ sudo vim /etc/apache2/httpd.conf
and add the following lines
<IfVersion >= 2.4> LoadModule ratelimit_module modules/mod_ratelimit.so </IfVersion>
2. Restart Apache Server
Restart Apache web server to apply the changes
sudo /etc/init.d/apache2 restart
3. Limit Download Speed
If you want to limit bandwidth for a specific URL, open your Apache server’s virtual host file and add the following rate limiting block of code as shown below. Customize it as per your requirement by changing the parts in bold
<IfModule mod_ratelimit.c> <Location /downloads> SetOutputFilter RATE_LIMIT SetEnv rate-limit 350 </Location> </IfModule>
Save and quit the file. Restart Apache Web Server to apply changes.
4. Test Bandwidth limiting
Place a big file at your location (e.g /downloads) whose bandwidth has been throttled and try downloading it multiple times.
Monitor the result