Varying by device type

Ruslan Sivak russ at vshift.com
Thu Oct 14 06:26:31 CEST 2010


  On 10/14/2010 12:00 AM, Michael Alger wrote:
> On Wed, Oct 13, 2010 at 11:48:52PM -0400, Ruslan Sivak wrote:
>> 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;
>>      }
>>     }
>> }
> Is that your entire/only vcl_recv? The default code, which is appended
> to your own statements, includes:
>
> #     if (req.http.Authorization || req.http.Cookie) {
> #         /* Not cacheable by default */
> #         return (pass);
> #     }
>
> which will obviously prevent any cache lookups being done if the client
> has sent a Cookie header. You'll want to do  return (lookup);  at some
> point to ensure the cache is checked, otherwise processing will go on
> to the default code and you'll get a 'pass'.
>
> The default vcl_fetch has similar code to return (pass) if the server
> has set a cookie, so you'll also want to override that.
>
So something like this then?

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;
     }
     return(lookup);
    }
}

sub vcl_fetch {
   set beresp.ttl=5d;
   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;
     }
     return (deliver);
    }
}
sub vcl_hash { if (req.http.Cookie) { set req.hash += req.http.Cookie; } }

It still doesn't seem to be working.

RUss





More information about the varnish-misc mailing list