Using varnish cookies to set up a/b test

sub vcl_recv {
 if (req.http.host == "www.domain.tld" && req.url ~ "^/pagename" && ! req.http.Cookie ~ "abtestpagename") {

  if (std.random(1, 100)>50) {
    set req.http.Cookie = "abtestpagename=newversion; Max-Age=xxxx; Path=/";
  } else {
    set req.http.Cookie = "abtestpagename=original; Max-Age=xxx; Path=/";
  }
 set req.http.X-Cookie-ab-test-pagename = req.http.Cookie;
}

 if (req.http.host == "www.domain.tld" && req.url ~ "^/pagename" && req.http.Cookie ~ "abtestpagename=newversion") {
  set req.url = "/newpagename/";
 }
//Other stuff that may include cookie cleanup for cache hit rate increase
}


sub vcl_deliver {
 if (req.http.X-Cookie-ab-test-pagename) {
  set resp.http.Set-Cookie = req.http.X-Cookie-ab-test-pagename;
 }
}

Comments

Popular Posts