Table of Contents

varnish

Varnish is a web accelerator.

Wikipedia: A web accelerator is a proxy server that reduces web site access times. They can be a self-contained hardware appliance or installable software.

Configuration

Edit /etc/varnish/default.vcl:

backend default {
  .host = "127.0.0.1";
  .port = "80";
}

The web server must accept connections on that IP address and serve the content. A standard virtualhost would listen only on public IP addresses.

Start varnishd:

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

Notes

  • Varnish can have several backends defined and can you can even join several backends together into clusters of backends for load balancing purposes.
  • 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.
  • You can use Varnish to cache content from different servers, effectively splitting URLs across many backends. Fex: http://domain/ pulls from 1.2.3.4:80 while http://domain/images/ pulls from 5.6.7.8:80.
  • Varnish has a “saint mode” where it will only send a cache if there is a bad request returnedV from the original webserver. Fex: Site returns HTTP 500 because it is overloaded, so Varnish serves stale content. See: here
  • Varnish can act as a high availability server, balancing load between two backends. Fex: Two servers on different IP addresses are handling the backend load. See: here

Ubuntu

curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add -
echo "deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0" >> /etc/apt/sources.list
apt-get update
apt-get install varnish