How to Varnish & HAProxy
My setup
[varnish] -- [haproxy] -- [nginx] -- [puma]
Varnish Cache
Why use Varnish?
- To reduce server load, especially CPU works.
- To make a website load faster, because cache stored in RAM.
- To gain more visitors.
Varnish is a web application accelerator. You install it in front of your web application and it will speed it up significantly.
Installation
Read more at http://www.servermom.com/install-varnish-3-to-run-with-apache-2-on-ubuntu-server/380/#mbiVGxmW1aChzKB2.99 Note: user precise repo for 12.04, 13.04
$ curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
$ echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get install varnish
$ sudo vi /etc/varnish/default.vcl
Make it looks like this
backend default {
.host = "33.33.13.31";
.port = "8080";
}
See example of configuration here. Open a file configured for startup varnish
$ vi /etc/default/varnish
Listen on port 80, administration on localhost:6082, and forward to one content server selected by the vcl file, based on the request. Use a 1GB fixed-size cache file.
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
Now you can test your server and it should work fine now. Using steps explained above your site will load faster and your server will be lighter in load. Also, by default as configured above in step 3, Vanish will use 256mb of your allocated RAM to store all cache files generated by the service. Please edit it to match your server specs. Edit that value to lower number If you are on a VPS with low amount of RAM. But you may also allocate x gb of your RAM if you are on Dedicated server.
To turn on varnishlog, edit this file and uncomment VARNISHLOG_ENABLED=1
$ sudo vi /etc/default/varnishlog
Usage
To start|stop|restart varnish
$ sudo /etc/init.d/varnish start|stop|restart
To view varnishlog to see if caching works properly
$ sudo varnishlog
(need to find out how to read it.)
To view varnishstat to see performance of cache
$ sudo varnishstat
(need to find out how to read it.)
To see if the webpage loaded via varnish, on Firefox, use Firebug > Net > (expand any request) > Header
You should see in the header:
Via 1.1 varnish
Read more at http://www.servermom.com/install-varnish-3-to-run-with-apache-2-on-ubuntu-server/380/#mbiVGxmW1aChzKB2.99
VCL (Varnish Configuration Languages)
The VCL language is a small domain-specific language designed to be used to define request handling and document caching policies
Why no SSL?
Varnish itself does not support (and with good reason), so we need another program to provide the secure connection.
HAProxy
Why HAProxy?
HAProxy has a huge list of features for reverse proxying that nginx hasn’t, varnish has the same for caching.
HAProxy as a balancer – it has more refined backend status/administrative page (http://demo.1wt.eu/ (without the admin features)). The nginx upstream module is lacking in this area
HAProxy have some features that Nginx still doesn’t have. Like backend max connections and frontend queue. So you can do throtlling to prevent your backend (DOS) and keep request from client in front. So the didn’t get HTTP 500.
Another feature is splice system call, which makes HAProxy really fast with low system load, that means the http payload not even touches the user-space, and the kernel just does a zero copy. Are you able to forward 20Gbps with nginx on a single machine?
Installation
On ubuntu 13.04 or later.
$ sudo apt-get install haproxy
$ haproxy -v
HA-Proxy version 1.4.18 2011/09/16
Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
To configure it.
$ sudo vi haproxy.cfg
To start|stop|restart.
$ sudo service haproxy start|stop|restart
To see if all the ports configured are listening.
$ netstat -antp | grep LIS