[Varnish] Stripping tracking cookies

Tracking tool, like google analytics, set cookies that are only used by their client side javascript. Theses cookies are no interest for your server, and depending your configuration their can event prevent cache usage. So it’s better to simply strip them.

To do that, add into the vcl_recv this block:

# Remove Google Analytics, Piwik and other tracking cookies
if (req.http.Cookie) {
   set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
   set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_pk_(ses|id)[\.a-z0-9]*)=[^;]*", "");
   set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *__utm.=[^;]+;? *", "\1");
}
# Remove cookies when empty
if (req.http.Cookie == "") {
   remove req.http.Cookie;
}

Further Reading and sources