Charly's Column – Mod_bw for Apache
Fighting the Heavyweights
A powerful web server will not help you much if too many users are competing for the bandwidth of its network connection. Sys admin columnist Charly came to a decision: The big data hogs get less favorable spots at his watering hole in future.
I have a web server with a 100Mbps connection that runs dozens of virtual hosts. One of them provides ISO images of Linux distributions, some of which are several gigabytes in size. If enough people try to grab them at the same time, the pipes get blocked – downloads push the remaining HTTP traffic off the road, Apache response times skyrocket, and users are annoyed.
My solution here goes by the name of libapache2-mod-bw , or similar, depending on your distribution. With this module, I can control the use of bandwidth (bw). Figure 1 shows an example with an excerpt from an Apache configuration that demonstrates the module's abilities.
BandwidthModule On enables bandwidth throttling for this virtual host. In contrast,
ForceBandwidthModule On looks like it might be redundant, but the line is definitely necessary; it forces all the connections through mod_bw – not just selected mime types.
Bandwidth lines state the maximum speed at which Apache sends its data to the defined clients. You need to watch out for the order here; the "first match" principle applies: The first matching bandwidth rule on a client wins, so you need to define the specific rules before the more generic ones.
If you define a maximum speed of zero in the configuration, as here in the first bandwidth line, there are no limits – the 10.50.0.0/16 network has free rein. For all other clients, the 20Mbps limit set in the next line applies. Watch out for the pitfalls, though! The speed must be set in bytes per second, which explains the value 2,621,440.
Hitting the Biggies
LargeFileLimit gives me the ability to throttle the speed for users downloading large files, whereas the host delivers small files at normal speed. I need to specify three parameters: The first is the file type, which is
.iso in this example; mod_bw would also accept an asterisk as a wildcard. This is followed by the file size in bytes; thus, typing 2,097,152 corresponds to 2MB. Finally, I set the speed for the heavyweights. I chose 5Mbps, which is expressed in bytes per second, or 655,360.
You also can limit the number of concurrent connections. The zero in the first
MaxConnection line means there are no limits for the 10.50.0.0/16 network, whereas an upper limit of 32 parallel connections is in force for all others. If the limit is exhausted, Apache normally responds with an error code of 503 (service unavailable), which is pretty nonspecific if you ask me. However, mod_bw gives you the option of specifying a different code; I decided to go for 510 and returning an error page I designed myself.
Charly Kühnast is a Unix operating system administrator at the Data Center in Moers, Germany. His tasks include firewall and DMZ security and availability. He divides his leisure time into hot, wet, and eastern sectors, where he enjoys cooking, freshwater aquariums, and learning Japanese, respectively.
Buy this article as PDF
New release marks the arrival of AMD’s unified driver strategy.
A new study by IDC charts big changes in the big hardware market.
Azure CTO says Redmond has already considered the unthinkable.
Lead developer quells rumors that the Debian version is slated for center stage.
MSBuild is now just another GitHub project as Redmond continues its path to the light.
Malware could pass data and commands between disconnected computers without leaving a trace on the network.
New rules emphasize collegiality in coding.
Upstart lands in the dust bin as a new era begins for Linux.
HP's annual Cyber Risk report offers a bleak look at the state of IT.
But what do the big numbers really mean?