Controlling bandwidth helps manage server resources efficiently. By limiting download speeds, no single client consumes excessive capacity, ensuring fair access and stable performance for all users. This approach is especially useful in shared hosting environments or networks with constrained throughput.

The Apache web server supports the mod_ratelimit module, which regulates outgoing traffic to prevent one client from monopolizing available bandwidth. Most Linux distributions ship this module, but it may require manual activation. Once enabled, it ensures that traffic stays within defined throughput boundaries.

Configuring mod_ratelimit involves setting thresholds for sustained data transfers and initial bursts. These parameters can be fine-tuned to create a balance between rapid content delivery and controlled resource usage. Proper testing helps confirm that the chosen rate limits align with performance goals.

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.