How to Use Varnish to Speed up my Website

How to Use Varnish to Speed up my Website

Caching is one of the best ways to maximize the output of your Linode. To achive that your server should not generate same dynamic data every time from scratch. Varnish is very helpfull to reduce your server load.

Varnish works BEFORE send HTTP request make to your server, Varnish check that if cache already generated for the HTTP request, if yes then it sends response from the cache and request will not make to the server, if not then it sends request to your server and then cache the output.

Installation

* First update current packages and install Varnish from apt (Linux):
1. apt-get update
2. apt-get install varnish

* Next thing we’ll do is edit the daemon options for Varnish to instruct it to load our custom configuration
nano /etc/default/varnish

## Alternative 2, Configuration with VCL
#
# Listen on port 80, administration on localhost:8080, 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:8080 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

*Varnish read VCL (Varnish configuration language) file and translates and compiles the VCL code into a C program that runs along side the Varnish process.
* Start configure varnish:
1. Basic example of

default.vcl (/etc/varnish/default.vcl)
backend
default {
    .host = "127.0.0.1";
	    .port = "80";
	}

2. Start the varnish service via “service varnish start”
3. As per the DAEMON_OPTS varnish runs on 8080 port, so access http://example.com:8080
4. To confirm the varnish implementation is working or not, you should check the Header of your HTTP Response. You should see below listed parameters
Via: 1.1 varnish
Age: 10