Varnish Web Cache Installation on RHEL

Varnish is a web cache and http accelerator. It is used improve the performance of dynamic websites by caching pages and then serving the cached version rather than dynamically creating them every time they are requested.

Install Varnish

Install Varnish from the Varnish repositories.

Add Varnish Repository

The first thing you need to do is add and enable the Varnish repository. Follow the link to install the correct version

Install the Varnish Application

[root@server ~]# yum install varnish

Configure Varnish to work with Apache

We now need to enable the configuration.

Enable Configuration

Open the varnish config file

[root@server ~]# vi /etc/sysconfig/varnish

Scroll down to the “Alternative Configurations”. The easiest way to configure Varnish is to enable configureation 2. Comment out with a # all the other alternative configurations. The configuration should look like the below snippet.

## Alternative 2, Configuration with VCL
# Listen on port 80, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request. Use a
# fixed-size cache file.
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"

Line 7 tells Varnish to listen on port 80 for web traffic. Line 8 tells Varnish to listen on localhost port 6082 for admin traffic. Line 9, tells Varnish to load the default.vcl. Line 10 is the user and group to varnish under. Line 11 is the Varnish secret key. Line 12 is what method for Varnish to store the cached information and to what size to allow it to grow.

Configure Default VCL

Open the default vcl file.

[root@server ~]# vi /etc/varnish/default.vcl

edit the “backend default” section to look like the below.

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

This tells Varnish to send all traffic to localhost ( on port 8080. This is the port and ip that apache will be listening on.

Configure Apache to work with Varnish

Next we need to configure Apache to work with Varnish.

Configure Apache (Main)

Open the apache config file

[root@server ~]# vi /etc/httpd/conf/httpd.conf

Change the “Listen” line to the following

# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, in addition to the default. See also the
# directive.
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (

This makes Apache listen on on port

Configure Apache (Virtual Hosts)

If you run virtual hosts on apache you will also need to reconfigure them to listen on on port 8080 too. Change the “NameVirtualHost” to look like this


You will also need to change each Virtual Host section to listen on on port 80. Below is an example.

        ServerName domain.tld
        ServerAdmin webmaster@domain.tld

        DocumentRoot /var/www/domain.tld/htdocs
        ErrorLog /var/www/domain.tld/logs/www.domain.tld.error.log
        CustomLog /var/www/domain.tld/logs/www.domain.tld.access.log combined

Forward User IPs to Logs

You may have seen that the web servers logs only display as the source IP. This causes problems when you need to run stats on the log file, as you loose quite a bit of information from loosing the IPs. This is quite an easy fix.

Update default VCL

Open the default.vcl

[root@server ~]# vi /etc/varnish/default.vcl

You need to update the default vcl with the below code. This will forward the source IP.

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

sub vcl_recv {
  remove req.http.X-Forwarded-For;
  set req.http.X-Forwarded-For = client.ip;

Apache Custom Log

We need to create a custom log to deal with the information from Varnish.

Create the following file

[root@server ~]# vi /etc/httpd/conf.d/varnish-log.conf

with the following content

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnishcombined

Update Web Hosts

You will now need to update the web hosts to state that the log format will be “varnishcombined” below is an example.

        ServerName domain.tld
        ServerAdmin webmaster@domain.tld

        DocumentRoot /var/www/domain.tld/htdocs
        ErrorLog /var/www/domain.tld/logs/www.domain.tld.error.log
        #CustomLog /var/www/domain.tld/logs/www.domain.tld.access.log combined
        CustomLog /var/www/domain.tld/logs/www.domain.tld.access.log varnishcombined

As you can see from the example above, the old “CustomLog” is now commented out and the new “CustomLog” with the varnishcombined entry is active.

Restart Services

Restart Apache

[root@server ~]# /sbin/service httpd restart

Restart Varnish

[root@server ~]# /sbin/service varnish restart

Set Auto Start

Auto Start Apache

[root@server ~]# /sbin/chkconfig httpd on

Auto Start Varnish

[root@server ~]# /sbin/chkconfig varnish on

Thats it you now have a working Apache Web Server fronted with a Varnish Web Cache.