How to limit connection bandwidth in Apache
Managing bandwidth is crucial for servers, especially when multiple clients download large files simultaneously. By controlling the speed or throttling download speeds, you can provide a balanced and efficient distribution of resources, ensuring all users receive optimal performance.
Apache, one of the most popular web servers, provides a range of modules and configurations allowing web administrators to control and throttle download speeds. One commonly used module is mod_ratelimit. When enabled and correctly configured, this module allows you to set a cap on the download speed for clients.
While many distributions come with mod_ratelimit, you might need to enable or configure it to suit your server's requirements. Below, you'll find steps to enable and configure download speed throttling on your Apache server.
Steps to throttle download speed on Apache:
-
Launch terminal.
-
Enable ratelimit module for Apache.
$ sudo a2enmod ratelimit # Ubuntu, Debian and SUSE Considering dependency env for ratelimit: Module env already enabled Enabling module ratelimit. To activate the new configuration, you need to run: systemctl restart apache2
Distribution with a2enmod support can simply run the command above without having to manually enable the required modules.
Options Debian, Ubuntu openSUSE and SLES Fedora Core, CentOS, RHEL macOS homebrew xampp a2enmod support yes yes no no no no Modules to install none Module name n/a ratelimit Loadmodule directive n/a LoadModule ratelimit_module <module_locations>/mod_ratelimit.so -
Open your Apache configuration file or virtual host file using a text editor.
$ sudo vi /etc/apache2/sites-available/000-default.conf
-
Add mod_ratelimit options within specific Location, Directory or VirtualHost directive where you want the bandwidth to be limited.
<VirtualHost *:80> # ..... # .... SetOutputFilter RATE_LIMIT SetEnv rate-limit 512 SetEnv rate-initial-burst 1024 </VirtualHost>
The above configuration limits download speed to 512 KB/s after an initial burst of 1MB. Adjust the values as required.
Related: Location for Apache VirtualHost configuration
Documentation: Offcial documentation for mod_ratelimit -
Save the file and close the text editor.
-
Restart Apache to apply the changes.
$ sudo systemctl restart apache2 # Ubuntu and Debian $ sudo systemctl restart httpd # CentOS and Red Hat
-
Download a file from the server to see if the bandwitdth is correctly limited.
$ curl --output image.iso onlineweb.tools/image.iso % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 3 512M 3 19.7M 0 0 538k 0 0:16:13 0:00:37 0:15:36 508k