You are on page 1of 10

Varnish

Programmers manual

What is Varnish
Varnish is an HTTP accelerator designed for content heavy dynamic web sites. It is a layer between HTTP frontend and application server(apache in our case). Popular client for varnish includes : Facebook,Globo,The Hindu etc. What is it not ? . Varnish is not replacement of other php performance tool like memcache, APC etc. varnish is just an addition to it.

Key Features
Fully Configurable Heavily Threaded Multiple backend handling
Serving assets (images/css/swfs...) from a lightweight backend whilst serving content from main server

Support Load Balancing Maintainance-mode

What are the cons?


Statistics :- as page served by varnish wont hit backend server so traditional server side analytics tool wont work. Need to use client side analytics tool e.g google analytics Caching-rules complexity :- configuring what to cache, which page is to cache etc involves programming complexity and need to write complex logic . Cache-Miss :- on event of cache-miss page served by varnish is usually slower then direct backend server.

Apache Benchmark
Apache benchmarking done on our local server

Varnish basic Commands


To Start Varnish : varnishd -f /usr/local/etc/varnish/default.vcl -s malloc,1G -T 10.157.222.71:2000 -a 10.157.222.71:2001

This will start varnish at port 2001. To Stop : pkill varnishd To Edit Varnish Default vcl file :Vim /etc/varnish/default.vcl

VCL
The VCL language is a small domain-specific language designed to be used to define request handling and document caching policies for the Varnish HTTP accelerator.

Backend declarations A backend declaration creates and initializes a named backend object:

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

VCL Sub Routines


1. vcl_recv Called at the beginning of a request, after the complete request has been received and parsed. Its purpose is to decide whether or not to serve the request, how to do it, and, if applicable, which backend to use. 2. vcl_pipe Called upon entering pipe mode. In this mode, the request is passed on to the backend, and any further data from either client or backend is passed on unaltered until either end closes the connection. 3. vcl_pass Called upon entering pass mode. In this mode, the request is passed on to the backend, and the backend's response is passed on to the client, but is not entered into the cache. Subsequent requests submitted over the same client connection are handled normally. 4. vcl_hit Called after a cache lookup if the requested document was found in the cache. 5. vcl_miss Called after a cache lookup if the requested document was not found in the cache. 6. vcl_fetch Called after a document has been successfully retrieved from the backend. 7. vcl_deliver Called before a cached object is delivered to the client.

Purging of cache
To purge varnish cache edit vcl : acl purge { "localhost"; "10.157.221.82"; "10.157.221.52"; "10.157.222.71"; } sub vcl_hit { if (req.request == "PURGE") { #set obj.ttl = 0s; ban_url(*); error 200 "Purged."; } } sub vcl_miss { if (req.request == "PURGE") { error 404 "Not in cache."; } } In vcl_fetch if(req.request == "PURGE"){ if(!client.ip ~ purge) { error 405 "Not allowed"; } } Bypassing CACHE if(req.url == "/varnishtest/randomnocache.php"){ return (pass) ; }

Thanks
Team Ninja
Anshuman ravi Anshuman.ravi@indiatimes.co.in

You might also like