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
-
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.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.