Wordpress

為什麼我在 Varnish 中的平均記憶體命中率很低?

  • March 22, 2012

任何人都知道為什麼在使用 Wordpress(使用 Varnish 和 Nginx)時我們的平均記憶體命中率非常低?

清漆統計顯示:

命中率:10 100 518 平均命中率:0.4143 0.2690 0.1948

知道如何解決這個問題嗎?我的清漆 default.vcl 是:

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition.  Set this to point to your content
# server.
#
backend default {
   .host = "77.81.240.177";
   .port = "8080";
}

acl purge {
   "77.81.240.177";
}

sub vcl_recv {

   # Add a unique header containing the client address
   remove req.http.X-Forwarded-For;
   #set    req.http.X-Forwarded-For = client.ip;
   set    req.http.X-Forwarded-For = req.http.rlnclientipaddr;    

   # Let's make sure we aren't compressing already compressed formats.
   if (req.http.Accept-Encoding) {
       if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|mp3|mp4|m4v)(\?.*|)$") {
           remove req.http.Accept-Encoding;
       } elsif (req.http.Accept-Encoding ~ "gzip") {
           set req.http.Accept-Encoding = "gzip";
       } elsif (req.http.Accept-Encoding ~ "deflate") {
           set req.http.Accept-Encoding = "deflate";
       } else {
           remove req.http.Accept-Encoding;
       }
   }

   if (req.request == "PURGE") {
       if (!client.ip ~ purge) {
           error 405 "Not allowed.";
       }
       return(lookup);
   }

   if (req.url ~ "^/$") {
       unset req.http.cookie;
   }
}

sub vcl_hit {
   if (req.request == "PURGE") {
       set obj.ttl = 0s;
       error 200 "Purged.";
   }
}

sub vcl_miss {
   if (req.request == "PURGE") {
       error 404 "Not in cache.";
   }

   if (!(req.url ~ "wp-(login|admin)")) {
       unset req.http.cookie;
   }

   if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.|)$") {
       unset req.http.cookie;
       set req.url = regsub(req.url, "\?.$", "");
   }

   if (req.url ~ "^/$") {
       unset req.http.cookie;
   }
}

sub vcl_fetch {
   if (req.url ~ "^/$") {
       unset beresp.http.set-cookie;
   }

   if (!(req.url ~ "wp-(login|admin)")) {
       unset beresp.http.set-cookie;
   }

   if (req.url ~ "^/w00tw00t") {
       error 403 "Not permitted";
   }
}

一個好的方法是使用 varnishlog 檢查流量,以了解適用哪些規則的原因。

由於您沒有強制執行 TTL,因此我的猜測是 wordpress 站點提供 Cache-Control 標頭,例如“max-age: 0”或“no-cache”“private”之類的,除非您特別指定,否則 varnish 會尊重此類標頭告訴它不要,即:

sub vcl_fetch {
    if (!(req.url ~ "wp-(login|admin)")) {
        unset beresp.http.set-cookie;
        set beresp.ttl = 3600s;
    }

vcl_fetch 中的第一條規則沒有意義,因為第二條規則將取消設置除 wp-login/wp-admin 之外的所有 url 的所有 set-cookie。

將一些配置添加到 vcl_deliver 以輸出標頭,說明它是否成功:

sub vcl_deliver {
       if (obj.hits > 0) {
               set resp.http.X-Cache = "HIT";
       } else {
               set resp.http.X-Cache = "MISS";
       }
    return (deliver);
}

對您配置的其他回饋:

在 vcl_recv 中,如果 url 不是 wp-login 或 wp-admin,則執行“unset req.http.cookie”,這意味著如果使用者登錄 /wp-login 然後轉到“/”,您將取消設置他的 cookie。您只需要在 vcl_recv 中為不需要的 url 設置 cookie,即 .(js|css|png|jpg) 等,而是刪除 vcl_fetch 中的 set-cookie。

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