Posted on

Varnish

Varnish is pretty cool speeding up web access but there are a couple of things to consider. Here are just some things I found and I don’t want to miss again.

Installation

# Varnish 3.0 - created 2013-08-28 17:20 UTC
# http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/
[varnish]
name = varnish
baseurl = http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/
gpgkey=
gpgcheck=0

# yum install varnish

# /etc/init.d/varnish start

Start at system startup

# chkconfig --level 345 varnish on

Status for Varnish

# varnishstat

Troubleshooting Varnish startup problems

If there are some issues starting up varnish it is very useful to see the compilation results:

# varnishd -C -f default.vcl 

Here an example of a not working vcl configuration:

Message from VCC-compiler:
Unused backend examplecom, defined:
('input' Line 23 Pos 9)
backend examplecom {
--------#########--

Configuration files of varnish

Startup script: /etc/init.d/varnish

System wide configuration: /etc/sysconfig/varnish (on RHEL/CentOS/Fedora machines) or /etc/default/varnish (on Ubuntu and Debian systems)
VCL configuration file: /etc/varnish/default.vcl

Logfile for Varnish

Docu Varnish NCSA.

varnishncsa -a -w /var/log/varnish/varnishstats.log -F "%t %U %{Varnish:time_firstbyte}x %{Varnish:hitmiss}x" &

Get the real IP address into Apache logs when using Varnish

The varnish configuration in /etc/varnish/default.vcl :

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

This works only for Varnish version 3, for older versions “client.ip” has to be replaced by “req.http.rlnclientipaddr”.

The logformat configuration at the virtual host /etc/httpd/vhosts.d :

<VirtualHost *:8080>
    ServerName www.example.com
    [...]
    # add the real ip from varnish into the log files
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnishcombined
    CustomLog /var/log/httpd/example_access_log varnishcombined

By the way, NCSA comes from the National Center for Supercomputing Applications (NCSA) where this log file format comes from.

Migrating from Varnish V3 to Varnish V4

Here a great list about what stuff to take care:
https://github.com/fgsch/varnish3to4