Introduction:
In order to limit the number of simultaneous connections to a Web server (for the whole server) the following Core directive is used in our Live web servers, limiting it to 2000 simultaneous connections per web server.
MaxClients 2000
In the case of wanting to limit the number of simultaneous connections per VirtualHost we use the module mod_bw. There is also another module called mod_slotlimit which could probably do the same work using an other principle, but for reason of lack of time I only describe the mod_bw here. To get information on installing and configuring the mod_slotlimit module see: http://www.debianadmin.com/manage-apache-resources-limits-with-mod_slotlimit.html or http://www.howtoforge.com/how-to-manage-apache-resources-limits-with-mod_slotlimit-debian-etch
Below is an example of such simultaneous connections limiting configuration with mod_bw.
I will explain in details each configuration directive:
Configuration:
Turn ON bandwidth limitation. Default is OFF
BandwidthModule On
Force limitation on every request, Default is that mod_bw doesn’t catch all the requests… don’t know why.
ForceBandWidthModule On
Necessary for the MaxConnection settings to work. Recommended value for this purpose.(100Mbit/s). More can also be set.
BandWidth all 102400000
Here is a feature where the purpose is not really that clear but seamimgly necessary. In this case it is understood as: The full above limited bandwidth(100Mbit/s) will be shared between each user down to the full limited bandwidth for a single user.
MinBandwidth all -1
The number of simultaneous requests processed by this vhost. No references to per IP or per client, just pure number of connections.
MaxConnection all 1000
I Chose a not-assigned error code: 510. Any ERROR code can be set here allowing it to mean: (404) File not found, (503) Resource unavailable, etc.
BandWidthError 510
It was recommended 510 be the author, to allow to use a specific delivery of an Error Document. eg:
ErrorDocument 510 PathToErrDoc.html
Note: It is also specified that this error document file size should be at least 1024 bytes for the Error Document to be delivered.
Documentation and module download:
For more information see the file mod_bw.txt (http://legacy.ivn.cl/files/txt/mod_bw-0.92.txt)
Downloading the module source code: http://ivn.cl/2010/01/06/downloads-for-bandwidth-mod/
Installation:
– In debian Squeeze:
apt-get install libapache2-mod-bw
– Or compile it as follows:(Thanks to the site: http://linuxadministration.us/?p=49)
wget http://ivn.cl/files/source/mod_bw-0.92.tgz
tar xvzf mod_bw-0.92.tgz
cd mod_bw
apxs2 -i -a -c mod_bw.c
You will probably 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..
It’s safe to ignore these above errors.
Configuration:
vi /etc/apache2/mods-available/mod-bw.load
LoadModule bw_module /usr/lib/apache2/modules/mod_bw.so
vi /etc/apache2/mods-available/mod-bw.conf
Add:
BandwidthModule On
ForceBandWidthModule On
BandWidth all 102400000
MinBandwidth all -1
MaxConnection all 1000
BandWidthError 510
ErrorDocument 510 /var/www/htdocs/510_Bandwidth_error.html
Set permissions
chown www-data /etc/apache2/mods-available/mod-bw*
Activate the module. (To deactivate use: a2dismod mod-bw)
a2enmod mod-bw
Restart apache2
/etc/init.d/apache2 force-reload
Note: The above use of the module is only to limiting the number of simultaneous connections. The module was originally meant for limiting the bandwidth and expanded into maximum connections.
See mod_bw.txt delivered with the tarball file for more installation/configuration info about it.