Posts Tagged ‘traffic shaping’

Traffic shaping using lighttpd

Wednesday, May 27th, 2009

Now that the server is using lighttpd its become possible to implement traffic shaping, you can even do it per directory which is a nice touch.

$HTTP["url"] =~ “^/photos/” {
connection.kbytes-per-second = 128
}

This limits all urls that start with /photos/ to 128k per second. You can try it on a photo of Queensland Australia that I’ve just uploaded. If you look at the output from wget we can see it in action:

Resolving www.idontplaydarts.com… 80.68.93.53
Connecting to www.idontplaydarts.com|80.68.93.53|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 13858463 (13M) [image/jpeg]
Saving to: `queensland.jpeg’

100%[=============================>] 13,858,463   136K/s   in 1m 46s

2009-05-27 15:44:02 (128 KB/s) – `queensland.jpeg’ saved [13858463/13858463]

And there we have it. Traffic shaping using lighttpd. There are some pitfalls – users can still open multiple connections to your URL using tools such as axel – instructions on installing and using axel on debian can be found on nixCraft

As you can see if we open 4 connections we get 4 times the throughput.

# axel -a -n 4 http://www.idontplaydarts.com/photos/panorama/queensland.jpeg
Initializing download: http://www.idontplaydarts.com/photos/panorama/queensland.jpeg
File size: 13858463 bytes
Opening output file queensland.jpeg.0
Starting download

Connection 3 finished                                                          ]
Connection 2 finished                                                          ]
Connection 0 finished                                                          ]
Connection 1 finished                                                          ]
[100%] [..................................................] [ 524.8KB/s] [00:00]

Downloaded 13.2 megabytes in 25 seconds. (524.79 KB/s)

Lighttpd version 1.5 is going to support a max connections per ip which would be handy if you wanted to prevent people opening multiple connections. Not really that handy for a website but possibly if your serving lots of large static files.