How to setup Varnish Cache a web application accelerator on CentOS 6.3

December 6, 2013

This is a how to article on setting up Varnish Cache on CentOS 6.3 64-bit as a single server web application accelerator (caching HTTP reverse proxy). Varnish Cache can increase the speed of your web application by 300 – 1000 times when used properly in the proper architecture.

First install your Varnish dependencies:

bash-shell# yum install -y gcc
bash-shell# yum install -y make
bash-shell# yum install -y automake
bash-shell# yum install -y autoconf
bash-shell# yum install -y libtool
bash-shell# yum install -y ncurses-devel
bash-shell# yum install -y libxslt
bash-shell# yum install -y groff
bash-shell# yum install -y pcre-devel
bash-shell# yum install -y pckgconfig
bash-shell# yum install -y libedit
bash-shell# yum install -y libedit-devel

Get your Varnish source code at the time of this blog Varnish 3.0.5 stable:

bash-shell# wget

Extract and compile Varnish

bash-shell# gunzip -c varnish-3.0.5.tar.gz | tar -xvf -
bash-shell# cd varnish-3.0.5
bash-shell# ./
bash-shell# ./configure
bash-shell# make
bash-shell# make check
bash-shell# make install

Your Varnish configuration file will be locate at /usr/local/etc/varnish/default.vcl

Below is a example default.vcl which configures Varnish to cache only what you determine and not everything else. The configuration is commented to explain the settings:


## Your backend http application server info as well as health checking
backend website {
  .host = "app.server.hostname";   # backend http application server or vip
  .port = "80";                    # port your backend application listens on
  .probe = {                       # health check setting 
    .request =
      "GET / HTTP/1.0"             # what to health check
      "Host: app.server.hostname"  # what host to health check
      "Connection: close";         # make sure to close the connection of the check
    .interval = 5s;                # how long time to wait between polls
    .window = 5;                   # how many of the latest polls to consider if its healthy
    .threshold = 2;                # how many of the .window polls must be good to be healthy
    .timeout = 1s;                 # how fast the probe must finish
    .expected_response = 200;      # expected http response of the health check
## Example cache only url beginning with /xyz and not everything else
sub vcl_recv {
  remove req.http.X-Forwarded-For;             # allowing client ip pass through
  set req.http.X-Forwarded-For = client.ip;
# cache url beginning with xyz and things ending in jpg css etc..  
  if (! req.url ~ "^/xyz/" || req.url ~ "\.(jpg|jpeg|css|js|png)$" ) {  
  unset req.http.Cookie;   # disregard caching cookies
  set req.grace = 1h;      # accept objects that are up to 1 hour old in case of backend failure
sub vcl_fetch {
  set beresp.do_esi = true;  # enable esi includes
  set beresp.ttl = 2m;       # time to live for cache that is not explict set from the header info
  set beresp.grace = 1h;     # deliver objects that are up to 1 hour old in case of backen failure

Now you can start Varnish with the below command, in this case it says to use the default.vcl with 3 Gigs of memory for cache, with administrative ip localhost and port 2000 and listen for any request on port 80

bash-shell#  /usr/local/sbin/varnishd -f /usr/local/etc/varnish/default.vcl -s malloc,3G -T -a

Some other commands to help you tune Varnish and debug it are:

# show varnish statistics
bash-shell# varnishstat
# show varnish logs
bash-shell# varnishlog
# clear all varnish cache
bash-shell# varnishadm "ban req.url ~ /"

To test it out put the url of your application site substituting the hostname for your new Varnish server, on your second load it should be a lot faster serving data from cache. You should use firebug to make sure or varnishstat command to see your cache hits vs misses. Remember in this configuration the things you explicitly state are only cached, and are what you will see the performance increase in.




http://varnish.server.hostname/xyz/my/app/is/awesome.php (should be faster after on second load)

Hope you find this helpful.

Comments are closed.