Apache: Bandwidth Limiter

Posted on May 19, 2009


Bandwidth Limiter Module

Bandwidth limiter module allows Apache web server to control over resources for each its virtual hosts. The module is an output filter in the Apache filters chain; each time a user requests data from the web server, the data (response) has to go through the module. The module breaks the data into smaller pieces and sends the pieces. And the module controls the bandwidth by introducing a small delay while sending the pieces. Looking at the module characteristics, the module is useful to limit access to predefined areas of the website and protect the web server from malicious users. It will significantly help small web-hosting servers with limited outbound bandwidth.


Applying 100 kB/s bandwidth limit for a vhost gives 100 kB/s download speed for the first client requesting the vhost. When the second client requests the vhost, the speed will be equally divided to 50 kB/s for each client. Of course, when the third client requests the vhost, the speed will then be equally divided to 33.3 kB/s for each client. This equal division will keep continue each time a new user requests the vhost.

Installing Apache mod_bw in Ubuntu Linux

Bandwidth module (mod_bw) is not a default option in the Apache installation under Ubuntu Linux. The following are the steps to have bandwidth module enabled in Apache web server:

  • (Optional) If you may want to download wget and apxs2 to be able to download and compile mod_bw.
    # sudo apt-get install wget apache2-threaded-dev
  • Download and compile mod_bw.
    # wget http://ivn.cl/files/source/mod_bw-0.8.tgz
    # tar xvzf mod_bw-0.8.tgz
    # cd mod_bw
    # apxs2 -i -a -c mod_bw.c

    I am assuming that you get this error:

    apxs:Error: Activation failed for custom /etc/apache2/httpd.conf file..
    apxs:Error: At least one `LoadModule’ directive already has to exist..

    Yes, it is expected. Just ignore the error.

  • Create the load file.
    # cd /etc/apache2/mods-available
    # sudo sh -c "echo 'LoadModule bw_module /usr/lib/apache2/modules/mod_bw.so' > bw.load"
    # cd ../mods-enabled
    # ln -s ../mods-available/bw.load bw.load
  • Add Bandwidth Configuration to your Virtualhost, for example:
    <Virtualhost *>
    Servername www.example.com
    BandwidthModule On
    ForceBandWidthModule On
    Bandwidth all 1024000
    MinBandwidth all 50000
    LargeFileLimit * 500 50000

    For more examples, please visit mod_bw setup.

  • Restart apache2 to activate the bandwidth module.
    # /etc/init.d/apache2 restart
Posted in: Technology