Apache Bandwidth Throttling | Go Back
This guide is written mostly for web administrators/developers, who want to control the bandwidth usage of their web resources. Especially in hosting environments, where bandwidth usage is very important, it’s vital that one resource does not eat up all of the available server bandwidth.
Apache Bandwidth Throttling
1) Who should use this guide?
Web admins and web developers who want to control the bandwidth usage of virtual hosts in Apache.
2) On what operating systems has this guide been tested on?
This guide was tested on Fedora/Redhat EL, but should work on any *nix or win32 environment as well.
3) Does this guide guarantee a proper bandwidth handling/throttling?
This guide is provided “as is” without any guarantees. The solution works pretty well for me, so there is no reason why it should not work for you. However, if you are experiencing problems and something does not work, please do not bug me with questions.
4) How does it work?
I use two modules to throttle bandwidth - mod_bandwidth and mod_limitipconn. mod_bandwidth controls overall bandwidth for a local folder content, while mod_limitipconn limits downloads per IP address. When used together, this is a very powerful combo and it does work very well! I used to run a site with mp3 and video downloads and it worked like a charm, saving me bandwidth and server resources. There are too many people out there with programs that run multiple threads for faster downloads. They would launch so many processes and crawl my site, that it would eventually slow down everything. After I introduced both modules on my server, all problems went away and I was able to put tough controls on downloads. People did complain, because their speeds went down and they could not download more than one file at a time, which was exactly what I wanted!
5) What you need to get started:
The guide is useless without a working Apache installation. If you don’t have Apache installed yet, go ahead and install it now. You can follow my “Apache, PHP, GD & Mod_Perl Guide” or install it on your own. I do not recommend running the default RPM version of Apache that comes with your OS distribution for various reasons. Go ahead and download the following modules into your source directory (in this case the source directory is /usr/local/src).
6) Modules that need to be downloaded into /usr/local/src:
- mod_bandwidth 2.0.6 from http://www.cohprog.com
- mod_limitipconn.c 0.04 with all patches from http://dominia.org/djao/limitipconn.html
7) Installing mod_bandwidth.c and mod_limitipconn-0.04:
Both modules can be compiled via Apache apxs.
# cd /usr/local/src
# tar zxf mod_limitipconn-0.04_patched.tar.gz
# /etc/httpd/bin/apxs -iac mod_bandwidth.c
# /etc/httpd/bin/apxs -iac mod_limitipconn-0.04_patched/mod_limitipconn.c
Once the above commands are executed, both mod_bandwidth and mod_limitipconn will be installed as modules in Apache. Check your httpd.conf and make sure that four new lines have appeared under LoadModule and AddModule for both mod_bandwidth and mod_limitipconn.
8) Apache configuration:
The next step is to configure Apache, so that the above modules are applied to a given folder within your server. Here is an example VirtualHost www.blah.com with a folder “movies”:
<VirtualHost 10.10.10.10>
DocumentRoot /home/httpd/site/www.blah.com
ServerName www.blah.com
BandWidthDataDir /home/httpd/site/www.blah.com/apachebw
<Directory "/home/httpd/site/www.blah.com/movies">
<IfModule mod_bandwidth.c>
BandWidthModule On
BandWidthPulse 1000000
BandWidth all 1048576
LargeFileLimit 1024 65536
MaxConnection 10
</IfModule>
<IfModule mod_limitipconn.c>
MaxConnPerIP 1
OnlyIPLimit audio/mpeg video
</IfModule>
AllowOverride None
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
For detailed documentation of the BandWidth directives, please see documentation.
First, I’m defining the “BandWidthDataDir”, which is a folder where mod_bandwidth stores temporary files. Make sure you create the folder “apachebw”, along with folders “master” and “link” inside the folder. Then, “BandWidthModule On” enables the mod_bandwidth module. “BandWidthPulse 1000000″ sends chunks of data every 1 second. “BandWidth all 1048576″ makes all IP addresses be limited to 1 Mb/second download speed for all files in the directory. “LargeFileLimit 1024 65536″ means for all files that are larger than 1024 kb, limit the bandwidth to 64 Kb/second. “MaxConnection 10″ means 10 simultaneous connections. No more than 10 downloads at a time will be permitted for this directory.
The next parameter “MaxConnPerIP 1″ forces mod_limitipconn module to accept only one download from an IP address at a time. The “OnlyIPLimit audio/mpeg video” directive limits this rule only to audio and video files. If you have other files that you would like to put a limit on, remove this line. You can also use “NoIPLimit directory/*” directive, which will remove the limitation from a particular folder.
That’s pretty much it. I hope this guide will resolve your bandwidth issues and save you some time and money! Dealing with leechers can be a real pain and these two modules will definitely keep them away.
February 8th, 2007 at 5:15 am
great work Nasim, thanks for putting some time in writing the howto’s.. Greatings from the Netherlands..
March 21st, 2007 at 11:26 am
Is there a win32 binary distribution of apache 2 bandwidth throttling? If all I want is a total upload cap for the httpd process, would it be easier to use a 3rd party firewall utility with throttling?