Varnish – Allow specific cookies for the backend

cookiesvarnish

I am trying figure out how to allow the backend to see tracking cookies on specific registration pages but ignore them on others. Currently, I'm trying to use a custom header sent from the backend to set beresp.ttl = 0s; inside the vcl_fetch:

if (beresp.http.cache-control ~ "max-age=-30") {
set beresp.ttl = 0s;
}

But the reg pages still prevent the backend from reading the cookie.

In the vcl_recv I am removing them by using this method:

set req.http.Cookie =
regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|_ga|_mkto_trk)=[^;]*", "");

set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");

if (req.http.Cookie ~ "^\s*$") {
unset req.http.Cookie;
}

How can I allow the cookie to get read on the backend on these specific pages? I assume I need to create exceptions in the vcl_recv?

Best Answer

Yes - you need to "return( miss );" on the requests in the backend. vcl_fetch is only triggered on 'fetching' an item from the backend - if an item is already stored in cache, varnish will not run the fetch code.