Press "Enter" to skip to content

Month: November 2009

Linux: Handy Varnish commands

If you are running the Varnish reverse proxy cache system on your website (yep, this is a techy post!), you might find the following command line tools useful. Varnish is a very powerful and useful cache tool which sits in front of your website and helps reduce the load on Apache/PHP – but there’s very little information about how to use it available. Hopefully this is the first of many posts about perfecting a varnish configuration. But first, let’s get an idea of some of the information that can be reported from varnish:

varnishlog
See what Varnish is currently processing.

varnishtop -i RxHeader -I \^Referer
Show the referer (sic) header for requests.

varnishtop -b -i TxURL
Shows requests made to the backend (-b) where the line matches (-i) the transmit URL (TxURL). Basically, shows you what is being passed to the backend and isn’t being cached. It will list all requests going to a backend, grouped by URL and sorted by a decaying average of frequency. Basically the number on the left should be single-digit and preferably all 1s or less (a higher number means the backend request is taking place frequently). [Technically, you don’t even need the “-b” as the TxURL is only set when making requests to the backend anyway]

varnishhist
Shows a histogram chart of the last 1,000 requests (by default) to the Varnish proxy showing “|” as a “hit” on the cache and “#” as a miss. The more “|” to the left of the chart, the better. The scale on the bottom is in seconds with 1e0 being “1” second and 1e-6 being 0.000001seconds (1e-1 being 0.1seconds). The vertical scale is shown in the top left hand corner.

varnishstat
Shows various information about varnish – I’m sure I’ll figure out what they mean in time…

Use varnishreload
From http://kristian.blog.linpro.no/2009/02/18/easy-reloading-of-varnish-vcl/ , this is a very handy script you can use to reload varnish’s configuration without having to restart the proxy server:
#!/bin/bash
# Reload a varnish config
# Author: Kristian Lyngstol
FILE="/etc/varnish/default.vcl"
# Hostname and management port
# (defined in /etc/default/varnish or on startup)
HOSTPORT="localhost:6082"
NOW=`date +%s`
error()
{
echo 1>&2 "Failed to reload $FILE."
exit 1
}
varnishadm -T $HOSTPORT vcl.load reload$NOW $FILE || error
varnishadm -T $HOSTPORT vcl.use reload$NOW || error
echo Current configs:
varnishadm -T $HOSTPORT vcl.list

Show what caused recent 503 errors
varnishlog -d -c -o TxStatus 503

Clear stale cache by host name
purge req.http.host ~ example.com

See what status codes are being commonly hit by your users (ideally lots of 200 and few 5xxx)
varnishtop -i TxStatus
See also: http://www.slideshare.net/schoefmax/caching-with-varnish-1642989,
http://blogs.osuosl.org/gchaix/2009/10/12/pressflow-varnish-and-caching/ and http://letsgetdugg.com/2009/12/04/random-varnish-tips-n-tricks/