How cookies affect Varnish caching?

不羁的心 提交于 2019-12-21 19:56:42

问题


How does Varnish caching affect third-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.


回答1:


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 .




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/5474863/how-cookies-affect-varnish-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!