[Varnish] #190: VCL Sample

Varnish varnish-bugs at projects.linpro.no
Tue Dec 18 10:23:30 CET 2007


#190: VCL Sample
---------------------------+------------------------------------------------
 Reporter:  michael.lee    |       Owner:  des  
     Type:  documentation  |      Status:  new  
 Priority:  normal         |   Milestone:       
Component:  build          |     Version:  1.1.1
 Severity:  normal         |    Keywords:       
---------------------------+------------------------------------------------
 We noticed that the default VCL Sample in VCL Man page is incorrect. It
 does not work when cookies are present. Looks like in order to cache
 dynamic contents when cookies are present, we need customize the
 vcl_hash() subroutine as well.

 Is it possible to include some well-tested vcl samples on wiki and VCL Man
 page?

 Here is an example inspired by this article -
 http://www.ipersec.com/index.php/2007/11/15/varnish-simple-and-fast-http-
 acceleration/

 {{{
 backend default {

         set backend.host = "localhost";

         set backend.port = "81";

 }

 sub vcl_recv {

     if (req.request != "GET" && req.request != "HEAD") {
         pipe;
     }

     if (req.http.Expect) {
         pipe;
     }

     if (req.http.Authenticate) {
         pass;
     }

     //ignore and pass no-cache documents
     if (req.http.Cache-Control ~ "no-cache") {

         pass;

     }

     /* lookup objects from cache even when cookies are present */
     lookup;

 }

 sub vcl_pipe {

     pipe;

 }

 sub vcl_pass {

     pass;

 }

 sub vcl_hash {

     /*
      * The hash subroutine is important
      * It defines the "name" pattern of
      * cache objects
      * We will use url+host by default.
      * In this way, different machines may
      * share the same cache object.
     */
     set req.hash += req.url;

     set req.hash += req.http.host;

     /*
      * You can also append cookie
      * to the hash. In this way,
      * varnish will fetch/insert a cache object for each
      * machine.
     */
     //set req.hash += req.http.cookie;

     hash;

 }

 sub vcl_hit {

     if (!obj.cacheable) {

         pass;

     }

     deliver;

 }

 sub vcl_miss {

     fetch;

 }

 sub vcl_fetch {

     if (!obj.valid) {

         error;

     }

     if (!obj.cacheable) {

         pass;

     }

     /* ignore set-cookie response */
     if(obj.http.Set-Cookie){

        pass;

     }

     /* ignores no-cache documents */
     if(obj.http.Pragma ~ "no-cache" ||
        obj.http.Cache-Control ~ "no-cache" ||
        obj.http.Cache-Control ~ "private"){

        pass;

     }

     /* insert documents to cache even when cookies are present */
     insert;

 }

 sub vcl_deliver {

     deliver;

 }

 sub vcl_timeout {

     discard;

 }

 sub vcl_discard {

     discard;

 }


 }}}

-- 
Ticket URL: <http://varnish.projects.linpro.no/ticket/190>
Varnish <http://varnish.projects.linpro.no/>
The Varnish HTTP Accelerator


More information about the varnish-bugs mailing list