Free Software

Cumulative bandwidth limiting module for Apache

This module was written to solve the problem of limiting the bandwidth usage of a hosted website.

Several small hosting companies will happily give you a co-located box with a set data transfer limit, such as '2Gb of traffic per month'. They will charge you extortionantly if you go over this limit...

I've looked around and I can't find a simple software solution to limit the data that Apache will serve, so I wrote one.

top
Alternative Solutions

The obvious solution is to store your files on a FTP server such as ProFTPd, and get Apache to redirect to that using mod_proxy, or something similar.

That's the only approach I could think of - things like mod_bandwidth, or mod_throttle just allow you to limit the current connections to prevent saturation - they don't keep track of the cumulative transfer over a given time period. (Yes mod_throttle does have 'Volume' limiting, but its not sufficient for my kind of limiting).

top
Limitations

This module will only work for complete servers, it will not handle virtual servers at all. Eventually be an Apache 2.x module implementing this functionality for virtual hosts, but I see no reason to tidy/release it prior to more widespread adoption of Apache2.

Once the bandwidth limit has been reached the server will redirect all further requests to a user specified URL. (Usually a static page which says something like 'Bandwidth exhausted, return in an hour').

To reset things you must restart the server. If the bandwidth limit is a few hundred Mb a day then simply setup a cronjob to do the necessary restart.

This module requires the use of mod_status, and 'ExtendedStatus On' to function normally.

top
Configuration Options

There are only a few configuration options to play with:

BandWidthExceededThe URL to redirect to if the bandwidth is exceeded.
BandWidthLimitThe maximum bandwidth to allow, eg '2Gb', '650Mb'
BandWidthMonitorURLA URL which may be used to monitor in real time the current bandwidth served, eg '/mod_curb/status'.

More options should be forthcoming to vary the timeframe for which allocations are possible...

top
Download

Download the source code for this module from the following URL:

If you are interested in following developments you might be interested in the mod_curb mercurial repository.

top
Building mod_curb

If you're running Debian, or RedHat Linux and you have got the 'apache-dev' package installed you should be able to build the module with a simple 'make -f Makefile-dev install'.

Otherwise rebuild Apache from source with something like this:

        ./configure --add-module=../mod-curb-1.0/mod_curb.c  \
        --enable-shared=curb --enable-shared=status --enable-module=so
	

A minimal configuration will look something like this:

	#  Bandwidth limiting requires mod_status
	LoadModule status_module libexec/mod_status.so
	ExtendedStatus on
	# Load the limiting code.
	LoadModule mod_curb libexec/mod_curb.so
	# Go here if the limit is exceeded
	BandWidthExceeded http://some.other.com/sorry.html
	BandWidthLimit 750Mb
	BandWidthMonitorURL /bandwidth-status/
	
top
Common Problems

If you visit the status URL you've setup and the bandwidth transferred stays at '0' then you've almost certainly not enabled mod_status, or included 'ExtendedStatus On' in your configuration file...

top
Feedback

If you have any feedback, or motivational inspiration to provide I'd be greatful to recieve it.

This is my first Apache module and I'd be grateful for any pointers or tips to writing better code.

top