How to Limit Bandwidth and Connections in Apache on CentOS

Channel: Linux
Abstract: Description of parameters. BandWidthModule is set to [On|Off] to enable or disable bandwidth on virtualhost. ForceBandWidthModule is set to [On|Off] t

The mod_bw Apache module provides the capability to limit bandwidth used by Virtualhost or limit max number of connections to any virtualhost. this can be helpful for those sites which provides large number of files to download for their users. This article will help you Setup limit bandwidth and connections in Apache on CentOS/Redhat systems.

1- Install mod_bw Module:

For yum based system, mod_bw is available under EPEL yum repository, So make sure you have enabled epel repository in system.

# yum install mod_bw
2- Limit Bandwidth in Apache:

After installing mod_bw module, Lets edit module configuration file /etc/httpd/conf.d/mod_bw.conf and add below line only, remove all other settings from file.

LoadModule bw_module modules/mod_bw.so

Now edit Apache Virtualhost and set the bandwidth like below.

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

Description of parameters.

BandWidthModule is set to [On|Off] to enable or disable bandwidth on virtualhost.
ForceBandWidthModule is set to [On|Off] to force bandwidth limit for each request made on virtualhost by end users.
BandWidth is use to set the limit on specific users or all users. This takes 2 parameters. From is the origin of the connections. It could be a full host, part of a domain, an ip address, a network mask (i.e # 192.168.0.0/24 or 192.168.0.0/255.255.255.0) or all. # The second parameter indicates the total speed available to the Origin.# If speed is 0, there is no limit.

After making all required configurations restart apache service.

# service httpd restart
3- Limit Connections in Apache:

To Limit maximum connection from all hosts for specific hosts we can use MaxConnection in virutalhost like below.

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

After making all required configurations restart apache service to take effect new chnages.

# service httpd restart

Ref From: tecadmin

Related articles