Use Varnish cache to get faster page load times

In this post we will show you how to install Varnish cache on your website.

Varnish cache is a web application accelerator used to load the web pages faster.

It is also known as a caching HTTP reverse proxy.

It is very useful in servers which hosts big websites with lot of images and has lot of traffic.

In this setup Varnish shows the cached contents (images and other static contents) and Apache serves the dynamic contents.

Varnish serves as the frontend which listens to the Port 80 (default HTTP port) and Apache servers the backend running in some other port (eg: 8080).

This tutorial deals with the installation and configuration of Varnish, assuming that Apache is already installed.

How to Install Varnish

Download and install Varnish official repo with rpm’ command.

rpm --nosignature -i

In the above ‘rpm’ command, ‘–nosignature’ is used to not verify package or header signatures when reading.

With the Varnish repo enabled, we can simply install Varnish with yum.

yum install varnish

Configure Varnish and Apache

After installing Varnish, we need to configure it in such a way that it listens to port 80 and Apache listens to any other port (in this case port 8080). For this, we need to edit the configurations of both Varnish and Apache to function in the way we need it to function.

Before editing the config files, we need to mention the port of Varnish daemon in /etc/sysconfig/varnish. Open the file and edit it as shown below.

DAEMON_OPTS="-a :80 \
 -T localhost:6082 \
 -f /etc/varnish/default.vcl \
 -u varnish -g varnish \
 -S /etc/varnish/secret \
 -s file,/var/lib/varnish/varnish_storage.bin,1G"

In the above code, port of Varnish is set to 80, configuration file of Varnish (/etc/varnish/default.vcl) and its storage binary file (/var/lib/varnish/varnish_storage.bin) is specified.

Now, open the configuration file of Varnish and set the Apache port. With this, for every request that requires dynamic content to be serverd, Varnish communicates with Apache in this specified port. Here is the code to be modified.

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

Here, Apache port is set to 8080.

Now, we have to make Apache listen to port 8080. For this, open the Apache configuration file (/etc/httpd/conf/httpd.conf) and edit the lines as follows.

Listen 8080
 NameVirtualHost *:8080

Now, restart the services for the chages to take effect.

/etc/init.d/varnish restart
 /etc/init.d/httpd restart

You can test if Varnish is working from your local machine with the following command.

curl -Is domainname

Refer the below given screenshot for the output.


If there is a line ‘Via: 1.1 varnish’ in the output, be sure the HTTP requests is handled by Varnish with Apache in the backend.

There it is! Now you now how to install Varnish for faster load times. If page speed is important for you then you will like our guide to speeding up your website.

Leave a Reply

Your email address will not be published.