Varying by device type

Michael Alger varnish at mm.quex.org
Thu Oct 14 06:00:34 CEST 2010


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.

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




More information about the varnish-misc mailing list