Controlling bandwidth is important for managing server resources efficiently. Limiting download speeds helps ensure that no single client consumes excessive bandwidth. This ensures fair access and stable performance for all users.

Apache provides the mod_ratelimit module to manage bandwidth. This module allows you to set limits on download speeds for clients. By configuring this module, you can prevent any single user from overwhelming your server.

Enabling and configuring mod_ratelimit is straightforward. Most Linux distributions include this module, but it may need to be manually enabled and adjusted to meet your specific needs. The following steps outline the process to limit download speeds using mod_ratelimit on an Apache server.

Steps to configure bandwidth rate limiting for Apache:

  1. Open the terminal.
  2. Enable the mod_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
  3. Open your Apache configuration file or virtual host file using a text editor.
    $ sudo vi /etc/apache2/sites-available/000-default.conf
  4. 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.

  5. Save the file and close the text editor.
  6. Restart the Apache service to apply the changes.
    $ sudo systemctl restart apache2 # Ubuntu and Debian
    $ sudo systemctl restart httpd # CentOS and Red Hat
  7. Test the configuration by downloading a file from the server.
    $ curl --output image.iso www.simplified.guide/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
Discuss the article:

Comment anonymously. Login not required.