开发者

How cookies affect Varnish caching?

How does Varnish caching affect t开发者_如何转开发hird-party cookies set by Omniture and first-party cookies set by Google Analytics?

So far I've seen opposing opinions, some people say that because the tracking stats are created by JavaScript (which is true), that stripping these Cookies from the request won't affect GA or Omniture.

However here they are saying that these softwares set various cookies to track recurring visitors, and so stripping these cookies from the request would essentially count every user as a new visitor.

I don't want my users to count every time as a new visitor. I am not sure either that these JavaScript embeds have the ability to calculate if the page is being served to a first time or recurring visitor. Any links to offical GA or Omniture documentation is deeply appreciated.


There's a good example here: https://www.varnish-cache.org/trac/wiki/VCLExampleRemovingSomeCookies

Basically you want to remove all GA cookies before Varnish sends the request to the backend with:

sub vcl_recv {
  if (req.http.Cookie) {
    set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *__utm.=[^;]+;? *", "\1"); # removes all cookies named __utm? (utma, utmb...) - tracking thing

    if (req.http.Cookie == "") {
        remove req.http.Cookie;
    }
  }
}

If no other cookies are left, Varnish will automatically cache and deliver pages to your visitors.


Google Analytics sets cookies through javascript on your domain. See How Google Analytics uses cookies :

Google Analytics uses only first-party cookies. This means that all cookies set by Google Analytics for your domain send data only to the servers for your domain.

For varnish usage, this means that you can cache page responses because personal cookies are not set through Set-Cookie headers but generated by javascript equal for everybody. The cookies will however be sent with requests to your domain, and the current default-vcl says:

sub vcl_recv {

  ..

  if (req.http.Authorization || req.http.Cookie) {
    /* Not cacheable by default */
    return (pass);
  }
}

It says it won't do a cache lookup if cookies are sent. So in your vcl_recv you should either 'unset req.http.Cookie' or do a lookup regardless of req.http.Cookie .


Google Analaytics sets and reads the cookies with JavaScript on the client side, so as long as you are not setting any conflicting cookies, Varnish caching doesn't affect it.

The cookies themselves might affect your Varnish caching, though -- as far as I remember, Varnish doesn't cache anything that has cookies and depends on them (that is, has a Vary: Cookie header). You might want to customize your VCL rules to make sure the Google Analytics cookies are not preventing caching.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜