Varnish shell singleliners: reload config, purge cache and test hits

Varnish is a server-side caching daemon. On our production server, Varnish listens to port HTTP 80 and serves at the production server front end cache; we use it mainly to serve JS, CSS and static images blazingly fast.

This blog post is based on default Varnish Ubuntu / Debian installation using apt-get. These instructions were tested on Ubuntu 12.04 LTS and Varnish 3.0.2.

1. Reload edited Varnish configs

Varnish caching rules live in /etc/varnish folder. The config entry point (main file) is /etc/varnish/default.vcl. The daemon itself (ports, etc.) is configured by /etc/defaults/varnish.

Varnish is controlled by an utility program varnishadm.You can use it in a console mode or issue direct command evaluations (think shell, MySQL client). On Ubuntu / Debian default installation varnishadm command as is is enough to control Varnish. However, on custom setup, you might need to guide it to a special console port or point it to a secret file.

Varnish config load is 2 stage process:

  • Parse and load cfg file to a Varnish memory and give it a handle you can later refer to it
  • Activate config by handle (only possible if step 1 success)

Below is an one liner shell script which generates a random handle and uses it to load the config if the config parses successfully.

HANDLE=varnish-cfg-$RANDOM ; \
  varnishadm vcl.load $HANDLE /etc/varnish/default.vcl && \
  varnishadm vcl.use $HANDLE

2. Purging Varnish cache from command line

Another useful snippet is to purge all Varnish cache from command line (invalidate all the cache):

varnishadm "ban.url ."  # Matches all URLs

Note: Command is purge.url in Varnish 2.x.

The cache is kept as shared memorymapped file in /var/lib/varnish/$INSTANCE/varnish_storage.bin. When Varnish is running it should map 1 GB (default) of your virtual memory to this file (as seen in ps, top).

You could also ban by a hostname:

varnishadm "ban req.http.host == opensourcehacker.com"

Here is a shell transcript where we observe that ban works as intended using wget utility.

# Go to /tmp because wget leaves files around
cd /tmp

# 1st load: uncached file, one X-Varnish stamp
wget -S http://opensourcehacker.com/wp-content/uploads/2011/08/Untitled-41.jpg

--2013-02-06 20:02:18--  http://opensourcehacker.com/wp-content/uploads/2011/08/Untitled-41.jpg
Resolving opensourcehacker.com (opensourcehacker.com)... 188.40.123.220
Connecting to opensourcehacker.com (opensourcehacker.com)|188.40.123.220|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: Apache/2.2.22 (Ubuntu)
  Last-Modified: Sun, 14 Aug 2011 22:55:01 GMT
  ETag: "2000893-108ec-4aa7f09555b40"
  Cache-Control: max-age=3600
  Expires: Wed, 06 Feb 2013 23:02:19 GMT
  Content-Type: image/jpeg
  Content-Length: 67820
  Accept-Ranges: bytes
  Date: Wed, 06 Feb 2013 22:02:19 GMT
  X-Varnish: 705602514

# 2st load: cached file, two X-Varnish stamps
wget -S http://opensourcehacker.com/wp-content/uploads/2011/08/Untitled-41.jpg
--2013-02-06 20:02:21--  http://opensourcehacker.com/wp-content/uploads/2011/08/Untitled-41.jpg
Resolving opensourcehacker.com (opensourcehacker.com)... 188.40.123.220
Connecting to opensourcehacker.com (opensourcehacker.com)|188.40.123.220|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: Apache/2.2.22 (Ubuntu)
  Last-Modified: Sun, 14 Aug 2011 22:55:01 GMT
  ETag: "2000893-108ec-4aa7f09555b40"
  Cache-Control: max-age=3600
  Expires: Wed, 06 Feb 2013 23:02:19 GMT
  Content-Type: image/jpeg
  Content-Length: 67820
  Accept-Ranges: bytes
  Date: Wed, 06 Feb 2013 22:02:22 GMT
  X-Varnish: 705602515 705602514

# Purge
varnishadm "ban.url ."

# It's non-cached again
wget -S http://opensourcehacker.com/wp-content/uploads/2011/08/Untitled-41.jpg
--2013-02-06 20:02:34--  http://opensourcehacker.com/wp-content/uploads/2011/08/Untitled-41.jpg
Resolving opensourcehacker.com (opensourcehacker.com)... 188.40.123.220
Connecting to opensourcehacker.com (opensourcehacker.com)|188.40.123.220|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: Apache/2.2.22 (Ubuntu)
  Last-Modified: Sun, 14 Aug 2011 22:55:01 GMT
  ETag: "2000893-108ec-4aa7f09555b40"
  Cache-Control: max-age=3600
  Expires: Wed, 06 Feb 2013 23:02:35 GMT
  Content-Type: image/jpeg
  Content-Length: 67820
  Accept-Ranges: bytes
  Date: Wed, 06 Feb 2013 22:02:35 GMT
  X-Varnish: 705602516

3. Restart Varnish on Ubuntu

This forces config flush, not sure about whether cache file storage gets reset(?).

service varnish restart

4. Further ideas

If someone knowns where to get Varnish VCL syntax highlighter for Sublime Text 2 (TextMate) that would make my life easier, used in the combination SFTP plug-in.

 

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

8 thoughts on “Varnish shell singleliners: reload config, purge cache and test hits

  1. ./parts/varnish-build/bin/varnishtop -b -i TxURL comes in handy in seeing what URLs actually get transmitted to the back-end (e.g. cache MISS), how frequently.

  2. Hi Ruben!

    VCL Syntax coloring works great.

    Installation instructions (OSX):

    cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/User 
    wget --no-check-certificate https://raw.github.com/zephirworks/Varnish.tmbundle/master/Syntaxes/VCL.tmLanguage
    

    Very useful especially when combined with SSH public key authentication and SFTP plug-in – you directly can test VCL configs on your server.

  3. Pingback: Varnish at the front of Wordpress + Apache and Plone CMS virtual hosts

  4. Reload vcl without a vcl file

    When varnish works with other system, vcl configure is dynamically generated by the front system and set with vcl.inline.
    There is not a vcl file any more. In this case, we have to use vcl.inline.

    the following is my varnish_vcl_reload shell script using vcl.inline. Any suggestions
    are welcome. — wilson.sun330@gmail.com
    ————————————————————————————————-
    #!/bin/bash
    #
    # reload active vcl
    # Sometimes, vcl_init needs to be re-run by reloading the active vcl.
    # For example, a geoip database is loaded in vcl_init. When the database is updated,
    # vcl_init needs to be re-run.
    #
    # This script saves the currently active vcl to a shell variable, and reloads it to varnish
    # Wislon Sun
    #

    usage=”$(basename “$0″) [-n ident] [-t timeout] [-S secretfile] -T [address]:port

    where:
    -h show this help text
    -n,t,S,T refer to varnishadm”

    while getopts ‘:n:t:S:T:d:h’ option; do
    case $option in
    n) ident=$OPTARG
    ;;
    t) timeout=$OPTARG
    ;;
    S) secretfile=$OPTARG
    ;;
    T) addressport=$OPTARG
    ;;
    h) echo “$usage”
    exit
    ;;
    🙂 printf “missing argument for -%s\n” “$OPTARG” >&2
    echo “$usage” >&2
    exit 1
    ;;
    \?) echo “illegal option: -$OPTARG” >&2
    echo “$usage” >&2
    exit 1
    ;;
    esac
    done

    # Done parsing, set up command
    VARNISHADM=”varnishadm”

    if [ ! -z “$ident” ]; then
    VARNISHADM=”$VARNISHADM -n $ident”
    fi

    if [ ! -z “$timeout” ]; then
    VARNISHADM=”$VARNISHADM -t $timeout”
    fi

    if [ ! -z “$secretfile” ]; then
    VARNISHADM=”$VARNISHADM -S $secretfile”
    fi

    if [ ! -z “$addressport” ]; then
    VARNISHADM=”$VARNISHADM -T $addressport”
    fi

    # Check if we are able to connect at all
    if ! $VARNISHADM vcl.list >> /dev/null 2>&1; then
    echo “Unable to run \”$VARNISHADM vcl.list\””
    exit 1
    fi

    # find current config
    current_config=$( $VARNISHADM vcl.list | awk ‘ /^active/ { print $3 } ‘ )

    # save current config to a variable
    vclcontent= $VARNISHADM vcl.show $current_config | sed ‘s/[“\]/\\\\&/g’ | sed ‘:a;N;$!ba;s/\n/\\\\n/g’

    timestamp=date +%Y%m%d_%H%M%S
    $VARNISHADM vcl.inline “vreload$timestamp” ‘”‘$vclcontent’”‘ >> /dev/null 2>&1
    vstring=varnishadm -n aeroflow vcl.list|grep “vreload$timestamp”
    if [ ! -z “$vstring” ]; then
    $VARNISHADM vcl.use “vreload$timestamp” >> /dev/null 2>&1
    $VARNISHADM vcl.discard $current_config >> /dev/null 2>&1
    echo “successfully reload current vcl config”
    exit 0
    else
    echo “failed to save current vcl config”
    exit 1
    fi

Leave a Reply

Your email address will not be published. Required fields are marked *