Monday, January 18, 2010

varnish and sessions / cookies disappearing fix

I was having a problem with logged in users behind varnish, this is what I did to fix it:

My php code:
$value = 'access';
setcookie("auth_user", $value);
setcookie("auth_user", $value, time()+86400);
setcookie("auth_user", $value, time()+86400, "/", ".mysite.com", 1);

and then in /etc/varnish/default.vcl:
sub vcl_recv {
if (req.http.cookie && req.http.cookie ~ "auth_user") {
    pass;
   }
}

sub vcl_hash {
if (req.http.cookie && req.http.cookie ~ "auth_user") { set req.hash +=
"auth"; }
    set req.hash += req.url;

 hash;
}

No comments:

Post a Comment