limit bandwidth & connection in apache

Limit Bandwidth & Connections in Apache

Apache is a popular web server used by many high traffic websites. If your website gets a lot of traffic, then it might be a good idea to limit the bandwidth & connections to your website, in order to prevent it from crashing, or avoid data loss due to large number of downloads. You can easily do this using mod_bw module in Apache. In this article, we will learn how to limit bandwidth & connections in Apache web server.


Limit Bandwidth & Connections in Apache

Here are the steps to limit bandwidth & connections in Apache. You can use mod_bw to limit bandwidth consumed by any virtualhost or the number of connections to it. You can use these steps for any RHEL/CentOS/Fedora/SUSE Linux systems.


1. Install mod_bw

Luckily, mod_bw is already available in EPEL yum repository. So you can easily download & install it with the following command.

# yum install mod_bw


2. Limit Bandwidth in Apache

Next, we need to edit its configuration file at /etc/httpd/conf.d/mod_bw.conf. So open it in a text editor.

$ vi /etc/httpd/conf.d/mod_bw.conf

Add the following line and remove all other lines from the file.

LoadModule bw_module modules/mod_bw.so

Next, open your website’s virtual host file and edit the VirtualHost tag. Add the lines below in bold. We have limited bandwidth to 1000 bytes/sec per IP address.

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

Let us look at the above configuration in detail.

  • BandWidthModule – Flag to turn on/off mod_bw. Takes values on/off to enable/disable mod_bw respectively.
  • ForceBandwidthModule – Flag to force bandwidth limit on each request made to virtualhost.
  • Bandwidth – Used to set bandwidth for one, some or all users. It takes 2 parameters. The first one is the origin of connections. It can be ip address, ip range, CIDR, domain, subdomain, network mask, or keywords like all, as shown above.

Please note, we have shown virtualhost that runs on port 80 (HTTP). If your website runs on port 443 (HTTPS/SSL), then you need to that virtualhost configuration file.


3. Limit Connections

You need to use MaxConnections directive to limit maximum number of connections to your website, as shown below. We have limited max connections to 10 simultaneous requests per IP address. You can change it as per your requirement.

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


4. Restart Apache Server

Restart Apache server to apply changes.

$ sudo service apache2 restart

In this article, we have learnt how to limit bandwidth & connections in Apache.

Also read:

How to Remove Untracked Files from Git
How to Create Menu with Submenu in Shell Script
How to Delete File in Git Repository
What Does __file__ mean in Python
Sed Command to Replace String in File

Leave a Reply

Your email address will not be published.