How to Limit Bandwidth Per Virtual Host in Apache


limit bandwidth using mod_bandwidth

mod_bw is a useful Apache module that allows you to limit bandwidth per virtual host in Apache web server. This helps you protect your web server from malicious attacks and save on data bandwidth costs. Here’s how to limit Bandwidth per Virtual Host in Apache.

 

How to Limit Bandwidth Per Virtual Host in Apache

Here are the steps to limit Bandwidth per virtual host in Apache. You can use these steps for CentOS/RHEL/Fedora systems.

 

1. Install mod_bw module

mod_bw is available in EPEL repository. So you can directly install it with the yum command

Make sure EPEL repository is enabled on your system


$ yum install -y epel-release

Install mod_bw module


$ yum install mod_bw

 

2. Limit Badnwidth in Apache

After installing mod_bw module, you can enable it by opening its config file in a text editor


/etc/httpd/conf.d/mod_bw.conf

 

and adding the following line in Apache config file


LoadModule bw_module modules/mod_bw.so

 

3. Update Virtual Host settings

Open your Apache virtual host file in a text editor and limit bandwidth as shown below


<Virtualhost *:80>
  ...
  ...
  <Directory "/var/www/html">
    BandWidthModule On
     ForceBandWidthModule On
     BandWidth all 1000 # in bytes/sec
  </Directory>
</Virtualhost>

In the above code,

  • BandWidthModule – enable/disable bandwidth on virtualhost
  • ForceBandWidthModule – force bandwidth limit for each request made to virtualhost
  • BandWidth – set the limit on specific users or all users. It takes 2 parameters.
    • From is the origin of incoming connections – could be a full host, part of a domain, an ip address, or even a network mask (i.e # 192.168.0.0/24 or 192.168.0.0/255.255.255.0) or all. #
    • Second parameter specifies the total speed available to the Origin. If it is 0, there is no limit

 

4. Restart Apache Web Server

Restart Apache Web Server to apply changes


$ service httpd restart

 

5. Limit Connections per Virtual Host

If you want to limit connections per virtual host, modify the virtual host config as shown below


<Virtualhost *:80>
  ...
  ...
  <Directory "/var/www/html">
    BandWidthModule On
    ForceBandWidthModule On
    BandWidth all 1000 # in bytes/sec
     MaxConnection all 10
  </Directory>
</Virtualhost>

 

Restart Apache Server again to apply changes


$ service httpd restart

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!