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 [1], 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.
Not surprisingly, 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.
The two 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
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.
Infos
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.