How to setup Varnish Cache

17 Dec
Goran

Goran

web developer at test
Goran

Latest posts by Goran (see all)

Varnish cache

What is Varnish?

Varnish is a caching tool/application. They call it also web application accelerator.
It runs on Linux. I will explain how I installed and configured Varnish for this blog.

So I am using digitalocean hosting with 64 bit Ubuntu 14.04. Hosting is very fast, personally I think it’s the best VPS on the world right now for given money.

Ok so site is very fast on digitalocean droplet by itself because it runs on SSD and it’s not shared hosting. I was just curious how Varnish works because I heard great things.

Why to use it and how it works

Varnish cache works like some kind of proxy. We install varnish on host server and adjust it to listen all traffic on port 80 (so all public HTTP traffic), than we setup it to communicate to our core server on port 8080 for example. And setup Apache or Ngnix to listen to 8080 (default is 80).

Varnish caches server response, that is static HTML, JS, images etc and than returns just that, a static content. It will work in some cases even if shut down apache.

So we can reduce server overload by a huge amount and speed up our site by also a huge percent.

How to setup Varnish Cache

It’s not so difficult to install varnish. It can be a bit heavy to tweak some advanced settings.

Ok so let’s go step by step. This is how I setup it on DOcean

  • Install it on UbuntuThis are instructions for ubuntu from official varnish site

     
  • Change default varnish port to 80

    You will see option DAEMON_OPTS – change it to 80.
  • Change default varnish port toward default server to 8080 (it can be other port if 8080 is taken)

    Be sure to set port to 8080. This will tell varnish to communicate with default server on port 80.
    backend default {
        .host = “127.0.0.1”;
        .port = “8080”;
    }

     

  • Now change apache setting to listen to 8080 port

    By default there is setting for port 80 like

    Liste 80

    replace this line with

    NameVirtualHost *:8080
    Listen 127.0.0.1:8080

    We should now change apache setting for our website to listen to 8080

    Change <VirtualHost *:80> to <VirtualHost *:8080>
    000-default.conf is a file for default site which is on /var/www/html
    This file can be yoursite.conf.

     

  • Restart default server and apache

You can use also command varnishlog to monitor varnish caching process and than             just reload your site.

Hack with ubuntu

In my case there was a problem with setup. I adjusted all settings but varnish used settings from other place so my setup for varnish ports was ignored.

Run this command to try to find other varnish config files on your system

in my case there was a file  /etc/systemd/system/multi-user.target.wants/varnish.service which had settings that I needed to change to settings like in /etc/varnish/default.vcl file.

Run this after to reload daemons.

So if you have a problem try to apply that fix. Details of this problem are here.

Wp varnish vcl

Varnish uses a VCL scripting for custom setups and requirements.
I just copied script from github for our wp blog.

Here is: https://gist.github.com/nadirlc/46987b42447cf8e3be79

In my case it  works. I hope we helped with you with setting up varnish cache. It’s a great tool.