Thursday, 5 February 2009

Limit the upload bandwidth of your apache webserver with mod_bw


I've a server at home on a ADSL connection which has 100kBytes/s of upload and 1.2MBytes/s of download as bandwidth.

If someone download at full speed some large file on my web server, my internet connection become unusable.

To avoid this, I start to ask my friends to use a download manager to limit their download speed... but it's somewhat far too tricky for a lot of them...

So I use mod_bw developed by Ivan Barrera http://www.ivn.cl/apache/ (sourceforge : http://bwmod.sourceforge.net/).

Here is a quick way to limit the bandwidth used by Apache :

Install & enable (on Ubuntu):
sudo apt-get install libapache2-mod-bw
sudo a2enmod bw


Configure (your apache virtualhost conf file like /etc/apache2/sites-available/default) :
<virtualhost *:80>

#... other config stuf for the virtual host

BandWidthModule On
ForceBandWidthModule On
BandWidth 192.168.0.0/24 0
BandWidth all 80000
</virtualhost>


Explaination :
  • line 1 : Activate the mod for the virtualhost.
  • line 2 : Set a directive so that the mod limit the bandwidth on all http request. (you can use an alternate config to limit only some mime type)
  • line 3 : set no limit for computer on your local area network.
  • line 4 : set a limit to 80KB/s to all other computer


Restart Apache so that the config is loaded and test :
service apache2 restart (or apachectl restart)


Test with Firefox on a large file. From my computer, I've first commented with a heading # the third line (BandWidth 192.168.0.0/24 0) and when it works, I uncommented it.

14 comments:

Mr. X said...

When you do the "configure" part, which file are you modifying? Is this a .conf file (bw.conf)? Also the virtualhost 80:* part of the configuration, should virtualhost or * be replaced with something?

I'm really new to Apache stuff, and this configuration is pretty important to me! Thanks.

Manson Thomas said...

the configure part is your apache2.conf file if your virtual host are declared directly in that file, or on ubuntu system, in a file found here : /etc/apache2/sites-available
(you should have a default file at least)

>virtualhost 80:*

It's a mistake I'll correct just after answering you.

it should be virtualhost *:80
where the * stands for 'any ip' (part of apache syntax).

If you replace * by a specific IP, apache2 will listen (serve html pages) only on this IP.

80 is the port.

ferdna said...

how do you calculate bandwidth? i want to split my bandwith 50/50 and only use 50% to my web server and the other 50% for my home.. how can i do the math on how much i will need?


thank you

Manson Thomas said...

well, it depends on what are you doing at home... (notice that we are talking about upload bandwith, which with ADSL is never greater that 1Mbit/s == 128KByte/s)

If you only do some browsing, 5 ot 10% of your upload bandwith is enough to send the request, the important thing is your download speed.

If you have something like emule, you need to spare more upload bandwith as you need at least 20kb/s to have full download capabilities on emule...

ferdna said...

this are my speeds:

Download:
11061 kbps

Upload:
495 kbps

Manson Thomas said...

well... limits your apache to 50kByte/s(400kbit/s) if you're only surfing, 35 if you're using emule...

ferdna said...

Download Speed: 11061 kbps
(1382.6 KB/sec transfer rate)

Upload Speed: 495 kbps
(61.9 KB/sec transfer rate)

so what should i put here:
BandWidth all 80000

80000 is in what?

Manson Thomas said...

80000 is in byte...

I've about 110kByte/s of upload...

so you should put 35000 or 50000

ferdna said...

so:
(uploadspeed in KB / 2) * 1024;

great thanks, you should update your post to reflect that that value is in bytes and should be accordingly to your upload speed...


thanks for the help...

Manson Thomas said...

no... it's not exact... look
i've 110kByte/s of upload and I set only to 80kByte/s...

it just depends on how you use your internet connection...

with 30kB/s i'm confortable for sending http request and uploading with emule

upload speed / 2 is wrong, it would be a waste of bandwith in my case as I don't need 55kB/s free to surf and emule

and in case of a low upload speed connection let's say 30kB/s, 15kB/s wouldn't be enough to have a correct emule upload and browsing...

ipserf said...

BandWidthModule On
ForceBandWidthModule On
BandWidth 192.168.0.0/24 0
BandWidth all 80000

I have limit only for download, but speed for upload file is not limited ?

Manson Thomas said...

No, it doesn't limit upload from internet to your server (and generally you don't need that).

Lasantha said...

Thanks man ,,,,really helps...

first it didn't installed in terminal. so i used synaptic by entering libapache2-mod-bw

installed changed the vhost.

working like a snail

Thanks man

Lasantha said...

Thanks man it works..

i installed from the synaptic by entering libapache2-mod-bw. im using ubuntu 11.04

now its working like a snail....:P