Menu

Getting started with Varnish cache

What is Varnish

Varnish cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture.

The keyword here is reverse proxy.
The Varnish cache is hit before your HTTP server and you can optimize the requests to your application server as you wish.

How to install

How to start

service varnish start will start the Varnish cache for you.
Navigate to http://127.0.0.1:6081/(replace with your hostname) to open the default varnish installation.

Setting up (part 1)

The default configuration can be found here:
/etc/varnish/default.vcl

Comments are good to read so have in mind that if using systemd you will need to cp /lib/systemd/system/varnish.service /etc/systemd/system/ and edit the new file. Otherwise copy the file as described below.

As you might know better copy the file and edit the copy than the original file directly. :-) cp /etc/varnish/default.vcl /etc/varnish/user.vcl
Then open the new file /etc/varnish/user.vcl. The .vcl extension means Varnish Configuration Language

This is where your original website will be accessible.
Most probably your website is accessible locally so let the 127.0.0.1 be your host. You may want to change the port to something else.
Have in mind that if you change to port to one that is already in use you should stop the application which is currently using this port.

Let's say your website runs on port 5000.

Using Varnish we will put the Varnish cache listen on port 5000 and then proxying all requests to port 6000 and change your website's listening port to 6000.

What we do in this section is telling Varnish to proxy all requests to the new website listening port. In our case we should change the port to 6000.

The backend section should look like this:

backend default {
.host = "127.0.0.1";
.port = "6000";
}

Setting up (part 2)

Now that we have configured Varnish to proxy the requests to our web server we need to configure it to accept requests on the desired port.

For Debian and Ubuntu the file is /etc/default/varnish while for Red Hat and CentOS it is /etc/sysconfig/varnish.
Open the file and here you will find

More than obvious these are the options which our Varnish server will use when starting.

First change the default.vcl with user.vcl which is our new configuration file we created earlier.

Second the -a option is the port Varnish will accept requests. In most of the cases we would want to accept HTTP requests on port 80 but in our case we said our website runs on port 5000 so we should change the port from the default 6081 to 5000. Change to: