Restoring cookies on a miss

John Norman john at 7fff.com
Mon Jan 25 22:03:25 CET 2010


Folks,

I've been trying to implement a technique posted here to restore
cookies on a cache miss.

The original question is here:

    http://projects.linpro.no/pipermail/varnish-misc/2010-January/003505.html

and an interesting answer is here:

    http://projects.linpro.no/pipermail/varnish-misc/2010-January/003506.html

Below is my .vcl file.

And again, here's the use case:

We have certain pages that should never be cached. The user comes in
with cookies set: "session=foo" or some such.

We strip the cookie and do a lookup.

If there's a hit, return what is in the cache.

If there's a miss, we'd like to fetch with the cookie.

Then, in fetch, pass for pages set for no-cache, and deliver for those
that are public.

It can be assumed that these pages are never hit first by non-cookied users.

But -- I am seeing a lot of requests that have no cookies on the backend.

John

Here's the VCL:

backend reviews0 {
  .host = "127.0.0.1";
  .port = "7000";
}

backend reviews1 {
  .host = "127.0.0.1";
  .port = "7001";
}

director reviews round-robin {
    { .backend = reviews0; }
    { .backend = reviews1; }
}

sub vcl_recv {

	unset req.http.user-agent;

	set req.backend = reviews;

  if (req.request != "GET" && req.request != "HEAD") {
    pass;
  }

  set req.http.OLD-Cookie = req.http.Cookie;
  unset req.http.cookie;

	lookup;
}

sub vcl_miss {
  if (req.http.OLD-Cookie) {
    set bereq.http.Cookie = req.http.OLD-Cookie;
    unset req.http.OLD-Cookie;
  }
  fetch;
}

sub vcl_fetch {

  unset obj.http.user-agent;

	if (obj.http.Pragma ~ "no-cache" ||
	  obj.http.Cache-Control ~ "no-cache" ||
	  obj.http.Cache-Control ~ "private") {
	    pass;
	}

	if (obj.http.Cache-Control ~ "public") {
    unset obj.http.Set-Cookie;
		deliver;
	}
	pass;
}



More information about the varnish-misc mailing list