Skip to Content

You are here

Quickstart guide Varnish on Ubuntu

Lets install Varnish :

Here we will consider the ubuntu as my OS is the same :P.

simply use.
#sudo apt-get install varnish

It will show the dependency tree for the varnish so just install.
In this case you don't have to build the packages and configure like other *nix.

Using varnish Servers :

Varnish has a concept of "backend" or "origin" servers. A backend server is the server providing the content Varnish will accelerate.

The default configuration file located at :
/etc/varnish/default.vcl

Now, this piece of configuration defines a backend in Varnish called default.
backend default {
.host = "127.0.0.1";
.port = "80";
}

Varnish can have several backends defined and can you can even join several backends together into clusters of backends for load balancing purposes.

I assume varnishd is in your $PATH. You might want to run pkill varnishd to make sure varnishd isn't running. Become root and type:

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

Visit followng url with your IP Address to get the web application of varnish:
http://IP-ADDRESS:8080/

 

Logging in Varnish :

One of the really nice features in Varnish is how logging works. Instead of logging to normal log file Varnish logs to a shared memory segment. When the end of the segment is reached we start over, overwriting old data. This is much, much faster then logging to a file and it doesn't require disk space.

varnishlog is one of the programs you can use to look at what Varnish is logging. When you check the log just after visiting your site with browser you will get the transmitting and receiving content. Tags starting with Rx indicate Varnish is recieving data and Tx indicates sending data.

Picking how much memory you should give Varnish can be a tricky task. A few things to consider:

* How big is your hot data set. For a portal or news site that would be the size of the front page with all the stuff on it, and the size of all the pages and objects linked from the first page.
* How expensive is it to generate an object? Sometimes it makes sense to only cache images a little while or not to cache them at all if they are cheap to serve from the backend and you have a limited amount of memory.
* Watch the n_lru_nuked counter with varnishstat or some other tool. If you have a lot of LRU activity then your cache is evicting objects due to space constraints and you should consider increasing the size of the cache.

Be aware that every object that is stored also carries overhead that is kept outside the actually storage area. So, even if you specify -s malloc,16G varnish might actually use double that. Varnish has a overhead of about 1k per object. So, if you have lots of small objects in your cache the overhead might be significant.

Put Varnish on port 80 :

First we kill off varnishd:
# pkill varnishd

Start up your web server and then start varnish:
# varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000

Tags: