Manage Apache Download Speed And Traffic Limits With mod_cband - Page 3

3.2 Configure Traffic Limits

In the next example we want to give our vhost www.example.com a traffic limit of 100MB per month, and if that limit is exceeded, we want to limit the download speed to 128kbps, max. 5 requests per second and max. 15 open connections:

<VirtualHost 1.2.3.4>
ServerName www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www
CBandLimit 100M
CBandExceededSpeed 128 5 15
CBandScoreboard /var/www/scoreboard
CBandPeriod 4W
</VirtualHost>

The directives are pretty self-explanatory. The CBandPeriod directive specifies after what time the traffic counter is reset to 0.

The next example is pretty similar, but now we don't want the speed to be limited if the traffic is exceeded, no, we get more restrictive and deliver a 503 status page instead:

<VirtualHost 1.2.3.4>
ServerName www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www
CBandLimit 100M
CBandScoreboard /var/www/scoreboard
CBandPeriod 4W
</VirtualHost>

If you want to redirect the user to a URL instead of presenting a 503 error to him, you can do it like this:

<VirtualHost 1.2.3.4>
ServerName www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www
CBandLimit 100M
CBandExceededURL http://www.someisp.com/traffic_exceeded.html
CBandScoreboard /var/www/scoreboard
CBandPeriod 4W
</VirtualHost>

Please note: the CBandExceededSpeed and the CBandExceededURL directives are mutually exclusive. They do not make sense in the same vhost!

After your changes, you must restart Apache:

Debian:

/etc/init.d/apache2 restart

Fedora:

/etc/init.d/httpd restart

Of course, you are free to combine the directives of chapter 3.1 and 3.2 in any way you like.


4 Status Page

To view the current bandwidth limits, usages, users, scoreboards, add the following Location lines to the vhost configuration:

<VirtualHost 1.2.3.4>
ServerName www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www
CBandLimit 100M
CBandExceededSpeed 128 5 15
CBandScoreboard /var/www/scoreboard
CBandPeriod 4W

<Location /cband-status>
SetHandler cband-status
</Location>
<Location /cband-status-me>
SetHandler cband-status-me
</Location>
</VirtualHost>

Restart Apache, and afterwards you can find the status pages under http://www.example.com/cband-status and http://www.example.com/cband-status-me:


5 Links

Share this page:

15 Comment(s)

Add comment

Comments

From: Anonymous at: 2006-06-03 20:10:48

mod_throttle

From: Anonymous at: 2006-06-01 15:42:21

The cband Apache2 module is available from Debian's package sources if you are running testing (etch) or unstable (sid). apt-get install libapache2-mod-cband

From: Anonymous at: 2006-06-07 20:54:35

libapache2-mod-cband is on backports.org for sarge.

You do not need to recompile.

From: Anonymous at: 2008-10-01 23:29:34

I have a test Apache 2.2 server on Windows 2003, is it poddible to get mod_cband to work on it?

 

 

From: Anonymous at: 2006-06-01 22:29:09

There is also a package for mod_cband in Fedora. Just install it with:

yum install mod_cband

From: Paki at: 2011-02-18 21:32:23

ThEnX! 

From: Anonymous at: 2006-06-02 13:34:26

Considering the majority of Apache installations are still using Apache 1.X, it's a shame that mod_cband isn't also available for Apache 1.X. I guess that the output filtering functionality of Apache 2.X (far superior to 1.X's) has caused this - one "workaround" might be to put an Apache 2.X reverse proxy in front of your (now backend) Apache 1.X server...

From: bahram at: 2011-01-07 10:53:32

hello, is there anyway to do global limitation for all accounts at once? I have about 500 account and I can not add limitation entry one by one.

From: Anonymous at: 2011-08-08 05:06:34

how to install mod_cband on windows 7?

From: Wedesign Marbella at: 2009-06-11 08:53:45

Hi,

 What would be the configuration to allow a site with only 80Gb per month?

Thanks in advance,

From: Lord Readman at: 2009-07-27 20:39:29

In response to Wedesign Marbella

#the scorebord of the virtual host
CBandScoreboard /var/www/scoreboard/site.scoreboard
#this would set the limit to 80GB
CBandLimit 80G
#this is the period of 30days (set to 31 if you wish)
CBandPeriod 30D
#if 80G is exceeded give everyone a 509 Bandwidth Exceeded Error.
CBandDefaultExceededCode 509

 Of course you can tailor your config to your custom requirements.

From: Wedesign Marbella at: 2009-08-06 14:34:16

Many thanks

From: Anonymous at: 2009-10-08 11:47:37

Hello, and thank you for writing this text.

How can I limit ips in downloads. ex: every ip can only download one time.

From: Anonymous at: 2013-04-10 18:07:52

I want use mod_cband with mod_proxy(apache2 with tomcat cluster). Any one has idea?

From: Anonymous at: 2006-08-27 11:47:45

hi,

accoridng the manual for cband the  CBandScoreboard is a path to a file. See bellow:

Name     CBandScoreboard
Description     Specifies virtualhost's scoreboard file
Context     <Virtualhost>
Syntax     CBandScoreboard path

in your examples it's defined as directory:

mkdir /var/www/scoreboard
chown apache:apache /var/www/scoreboard/

CBandScoreboard /var/www/scoreboard

which is not true!

you should touch the file not do mkdir. 

 Best regards,

Martin