Putting Varnish in front of your website

February 18, 2015

If you have a site with lots of anonymous traffic, Varnish is something you will like. Varnish is a reverse proxy cache. Varnish caching acts like a browser. It act on the header information served bij the webserver. Using Varnish will reduce the load of the webserver, as the request don’t reach the webserver.

Lets start off with installing Varnish.

$ apt-get install varnish

Now we will configure varnish to listen on the correct port and automaticly start at boot. Adjust /etc/default/varnish so it looks like this.

# Should we start varnishd at boot?  Set to "yes" to enable.

## Alternative 2, Configuration with VCL
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a 1GB
# fixed-size cache file.
DAEMON_OPTS="-a :80 \
         -T \
         -f /etc/varnish/default.vcl \
         -S /etc/varnish/secret \
         -s maloc,1G"

Now we can configuere the backend. Edit /etc/varnish/default.vcl

backend default {
    .host = "";
    .port = "80";

Restart varnish to give your site a try.

$ /etc/init.d/varnish restart

Now you can surf to the varnish server and you should see the site from you backend. You can debug this using Firefox with the firebug plugin. Open the NET tab and refresh you page. Open the document request and you should see a Varnish header.

If your X-Varnish header has only one integer, varnish is not caching your page. This is probably because you are sending/using cookies. Varnish’s default behavior is not to cache when cookie are set.

You can use Varnish to cache when cookies are set or to loadbalance request over multiple backends. There are numerous options and configurations. For more info check the varnish website .

comments powered by Disqus