Restoring cookies on a miss

John Norman john at
Mon Jan 25 22:03:25 CET 2010


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

The original question is here:

and an interesting answer is here:

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.


Here's the VCL:

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

backend reviews1 {
  .host = "";
  .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") {

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


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

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") {

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

More information about the varnish-misc mailing list