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
Kernel king admits his tone has alienated volunteers, but says the demands of the process require directness.
New flaw in an old encryption scheme leaves the experts scrambling to disable SSL 3
Lennart Poettering wants to change the way Linux developers talk to each other.
Enterprise giant frees itself from ink and home PCs (and visa versa).
Mozilla’s product think tank sinks silently into history.
TODO group will focus on open source tools in large-scale environments.
New tool will look like GParted but support a wider range of storage technologies.
New public key pinning feature will help prevent man-in-the-middle attacks.
Carnegie Mellon researchers say 3 million pages could fall down the phishing hole in the next year.
The US government rolls new best-practice rules for protecting SSH.