Cache

清漆正在記憶體登錄的使用者頁面並為這些頁面提供服務

  • December 20, 2014

我在使用者登錄時設置了 logged_in cookie。如果logged_in cookie 存在,那麼varnish 不會記憶體請求。

這是我的 vcl_recv

sub vcl_recv
{
   if (req.backend.healthy) {
       set req.grace = 30s;
   } else {
       set req.grace = 1h;
   }

   # Handle compression correctly. Different browsers send different
   # "Accept-Encoding" headers, even though they mostly support the same
   # compression mechanisms. By consolidating compression headers into
   # a consistent format, we reduce the cache size and get more hits.
   # @see: http:// varnish.projects.linpro.no/wiki/FAQ/Compression
   if (req.http.Accept-Encoding) {
           if (req.http.Accept-Encoding ~ "gzip") {
           # If the browser supports it, we'll use gzip.
           set req.http.Accept-Encoding = "gzip";
       }
       else if (req.http.Accept-Encoding ~ "deflate") {
           # Next, try deflate if it is supported.
           set req.http.Accept-Encoding = "deflate";
       }
       else {
           # Unknown algorithm. Remove it and send unencoded.
           unset req.http.Accept-Encoding;
       }
   }

   # Set client IP
   if (req.http.x-forwarded-for) {
       set req.http.X-Forwarded-For =
       req.http.X-Forwarded-For + ", " + client.ip;
   } else {
       set req.http.X-Forwarded-For = client.ip;
   }

   if (req.http.Authorization || req.http.Authenticate)
   {
     return (pass);
   }

   if (req.url ~ "^/registration" ||
       req.url ~ "^/blog/viewnoti/" ||
       req.url ~ "^/action/insert_user" ||
       req.url ~ "^/loginmanager.*$") {
       return (pass);
   }

   if (req.request != "GET" &&
     req.request != "HEAD" &&
     req.request != "PUT" &&
     req.request != "POST" &&
     req.request != "TRACE" &&
     req.request != "OPTIONS" &&
     req.request != "DELETE") {
         # /* Non-RFC2616 or CONNECT which is weird. */
       return (pipe);
   }

   if (req.request != "GET" && req.request != "HEAD") {
       # /* We only deal with GET and HEAD by default */
       return (pass);
   }

   if (!req.backend.healthy) {
       unset req.http.Cookie;
   }

   if (req.http.cookie ~ "logged_in") {
       return (pass);
   }

   if (req.http.Cache-Control ~ "(no-cache|no-store|private)") {
       return (pass);
   }

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

       if (req.http.cookie == "") {
           unset req.http.cookie;
       }
   }

   return (lookup);
}

這是我的 vcl_fetch

sub vcl_fetch
{
   if (req.url ~ "^/" ||
       req.url ~ "^/live" ||
       req.url ~ "^/selected" )
   {
       set beresp.ttl = 5m;
   } else {
     set beresp.ttl = 30m;
   }

   if (req.http.cookie ~ "logged_in") {
       set beresp.ttl = 0s;
   }

   if (req.http.Cache-Control ~ "(no-cache|no-store|private)") {
       set beresp.ttl = 0s;
   }

   # Set Grace Time to one hour
   set beresp.grace = 2h;
}

Varnish 正在記憶體登錄使用者的請求並將這些頁面提供給訪問者和其他登錄使用者。我不知道為什麼 t 這樣做。

您是否嘗試在阻止之前移動 cookie 檢查if (!req.backend.healthy) {(這可能會取消設置 cookie)?或者您可能希望(至少用於測試)為每個 cookie 製作記憶體(這樣它就不會為登錄使用者提供錯誤的內容):

sub vcl_hash {
   hash_data(req.http.cookie);
}

有關更精細的方法,請參閱文件。如果您已經有特定的東西,可能值得檢查 vcl_hash ……

引用自:https://serverfault.com/questions/646422