清漆:如何为带有 cookie 的动态页面添加例外

mnm*_*nml 2 configuration cache varnish

我想知道避免使用 Varnish 缓存网站的“某些页面”并缓存所有其他页面的正确方法是什么。

这就是我试图用 vcl conf 做的事情:

     sub vcl_fetch {
         #set beresp.ttl = 1d;
         if (!(req.url ~ "/page1withauth") ||
             !(req.url ~ "/page2withauth")) {
            unset beresp.http.set-cookie;
         }
         if (!beresp.cacheable) {
             return (pass);
         }
         if (beresp.http.Set-Cookie) {
             return (pass);
         }
         return (deliver);
}
Run Code Online (Sandbox Code Playgroud)

谢谢

BMD*_*Dan 5

通常,这将在 vcl_recv 中完成:

sub vcl_recv {
  if ( req.url !~ "^/page1withauth" && req.url !~ "^/page2withauth" )
  {
    unset req.http.Cookie;
    remove req.http.Cookie;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,您应该从服务器返回 set-cookie 参数的唯一时间是当您尝试唯一标识连接时。如果是因为他们刚刚发布或类似,那已经可以逃避缓存了。如果只是因为您只是想唯一地标识它们,那么问题在于您的应用程序代码故意破坏了 Varnish;如果可以,请修复您的应用程序,否则您可以覆盖 vcl_fetch 类似于您在此处执行的操作。