Last Updated: March 02, 2016
·
16.03K
· boris

Reload varnish config without restart

Yesterday I made a modification on my default.vcl file adding a new backed. The problem is: how to reload the config without restarting Varnish?

Easy: Using varnishadm like this:

# varnishadm
200
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
-sfile,-smalloc,-hcritbit

Type 'help' for command list.
Type 'quit' to close CLI session.

vcl.load newconfig01 /opt/local/etc/varnish.vcl
vcl.use newconfig01
quit
500
Closing CLI connection

The first line vcl.load newconfig01 /opt/local/etc/varnish.vcl tells to varnish to load the config file adding a name to the config (newconfig01). The second line vcl.use newconfig01 tells to varnish to use the new config.

After running this commands, the new backed was ready to use.

1 Response
Add your response

The method mentioned here reloads a vcl file into varnish. But there may be not this 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 varnishvclreload 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

over 1 year ago ·