Linux

Varnish HTTP Accelerator

For years we've used squid on a lot of projects, and in the past few years we have been installing varnish on clients sites.  More and more customers have also been asking about varnish. Initially in the earlier versions of varnish it was sometimes a bit tricky to explain to customers how to get it compiled and built as most of our customers are windows users with  servers in the Linux environment, however recently varnish has been offering RPM's for easy installation through yum.  I mention only yum here as the debian and other installations are equally as easy, and all of the architectures have installation instructions available here: http://www.varnish-cache.org/releases/varnish-cache-2.1.5 So once you follow the guides available above a lot of people get stuck and are confused about what to do even though there is a really good documentation system available.  So to get you an idea on where to start...  Once varnish has been installed via a yum install varnish, it should create a default.vcl file which lives underneath /etc/varnish/default.vcl Technically speaking the *ONLY* thing you need to have in this file at this point is:
backend default {
  .host = "127.0.0.1";
  .port = "8000";
}

Now you'll notice that the port here is listed at 8000, this means that Varnish will ask the apache or other web server that you have for information on port 8000. This means however that you will need to update your apache server or web server of choice to listen on port 8000. Once you've completed that, restart your web server to prevent it from taking up the default port 80. Now the final magic. Please note in the below example I have a backslash ( \ ) at the end of the first line, this is to create a double line input.

varnishd -f /usr/local/etc/varnish/default.vcl -s malloc,1G \
-T 127.0.0.1:2000 -a 0.0.0.0:8080

What the above line does is as follows: it starts varnish, and tells it that the configuration file it should use is default.vcl (-f /usr/local/etc/varnish/default.vcl) Now in our case the documentation is wrong, as varnish installed a file for us in /etc/varnish/default.vcl, so this needs to be updated. The second part to this is -s malloc,1G. Malloc means that varnish will be caching to memory and use up 1GB of space. Then there is the -T directive. This specifies where the administration telnet console will listen. You can once issuing the command run telnet localhost 2000 and log into varnish to perform several different tasks which at this point is out of our scope. The final and most confusing attribute to our users is the line that reads -a 0.0.0.0:8080. This line however complicated is completely unnecessary. What it does is to tell varnish to listen on address 0.0.0.0 on port 8080. Due to our web server previously running on port 80 by default we want it to listen on port 80 not 8080, and we want to keep it listening on all interfaces so we can now exclude this entire switch. So finally on our redhat based system with the default vcl file installed under /etc/varnish/default.vcl, and apache already listening on port 8000 all we need to issue is the following, followed by viewing our website provided by our http accelerator...

 varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000

One last thing before we let you go, if you are running on a 32bit system for whatever reason, it is not recommended to run varnish witth more than 1GB swap. Varnish was designed to run on 64bit systems and performs outstanding, however if you have to run on 32bit it will still work, however if you increase this limit too much then you will run into issues with the stack, as well as 32bit memory limits etc.

We hope you enjoyed this very basic but asked for tutorial

About the author