Varying by device type

Ruslan Sivak russ at vshift.com
Thu Oct 14 05:48:52 CEST 2010


  We would like to vary by device type.  We have code that detects this 
when the first page on the website gets hit and then sets a device_group 
cookie.

I would like to have varnish cache these pages based on the value of the 
cookie.  I can't seem to get it to work.  Here is my code:

sub vcl_recv {
    if (req.http.Cookie) {
     set req.http.Cookie = ";" req.http.Cookie;
     set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
     set req.http.Cookie = regsuball(req.http.Cookie, 
";(device_group)=", "; \1=");
     set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
     set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

     if (req.http.Cookie == "") {
         remove req.http.Cookie;
     }
    }
}

sub vcl_fetch {
   if (beresp.http.set-cookie) {
     set beresp.http.set-cookie = ";" beresp.http.set-cookie;
     set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, "; 
+", ";");
     set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, 
";(device_group)=", "; \1=");
     set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, ";[^ 
][^;]*", "");
     set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, "^[; 
]+|[; ]+$", "");

     if (beresp.http.set-cookie == "") {
         remove beresp.http.set-cookie;
     }
    }

         set beresp.ttl = 5d;
}

sub vcl_hash { if (req.http.Cookie) { set req.hash += req.http.Cookie; } }

It seems that this should work, however it's not caching.  How can I 
make it cache even though a cookie is set?

Thanks in advance,

Russ





More information about the varnish-misc mailing list