Varnish keeps missing cache ,cookies?

后端 未结 2 1721
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 23:55

I have a bit of trouble getting varnish to cache correctly.

When I go to a page and hit refresh , varnish will return the cached page. But if I use another computer

2条回答
  •  滥情空心
    2020-12-29 00:39

    This is because of Google Analytics. Even if your Web application does not use cookies your analytics JavaScript does. The result is the same, Varnish will pass the request to the back-end and avoid using it's cache.

    To fix this define URLs of your Web application where you have to use cookies (e.g. administration panel) and where you don't (Here you can ignore the requirements of Google Analytics. Most Web analytics tools need cookies only between the browser and the JavaScript.).

    Below you can see an example of a Varnish configuration file for this. The interesting part is that for non-administrative parts of your Web site: Sent cookies will be deleted as well as some browser request headers for fresh content.

    sub vcl_recv {
      # regex to find all URLs where cookies are required
      if (req.url ~ "^/admin/") {
        # administration panel
        set req.http.admin = 1;
      } else {
        # public web site, ignore client request for fresh content, remove cookies
        unset req.http.Cache-Control;
        unset req.http.Max-Age;
        unset req.http.Pragma;
        unset req.http.Cookie;
      }
      ...
    }
    
    sub vcl_fetch {
      if (req.http.admin == 1) {
        # administration panel
        return (hit_for_pass);
      } else {
        # public web site, not allowed to set cookies
        unset beresp.http.Set-Cookie;
        ...
      }
      ...
    }
    

提交回复
热议问题