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
“Xenial Xerus” comes with a new packages format and several improvements for the enterprise.
Linux users can now download and install the Windows code editor
New initiative will address security and interoperability concerns around container technology.
Developers can use RHEL as a development platform without a subscription fee.
Windows users will soon have native access to the Bash shell.
Improvements to SMTP will provide better guarantee of confidentiality
Graphics vendor embraces new reality in Linux graphics
Pioneer Ray Tomlinson bequeathed the @ sign to billions of Internet users
Redmond says its classic database tool will run without Windows
New intrusion technique affects most non-Bluetooth wireless mice