为什么我会在清漆上获得低的平均命中率?

任何人都知道为什么我们得到一个非常低的平均caching命中率,当使用Wordpress(与光油和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检查stream量,以查看哪些规则适用。

由于你没有执行一个TTL,我的猜测是,WordPress的网站提供像“max-age:0”,或“no-cache”“私人”或什么的caching控制头,和清漆荣誉这样的头,除非你特别告诉它不要,即:

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

vcl_fetch中的第一条规则没有意义,因为第二条规则将取消设置所有url的所有set-cookies,但是wp-login / wp-admin。

添加一些configuration到vcl_deliver来输出标题,说明是否命中:

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

其他反馈给你的configuration:

在vcl_recv中,如果url不是wp-login或者wp-admin,这意味着如果用户login/ wp-login然后进入“/”,你将取消设置他的cookie。 您只需要在vcl_recv中取消设置不需要cookie的cookie,即。(js | css | png | jpg)等,然后移除vcl_fetch中的set-cookie。

WP-清漆

Checkout wp-varnish – 一个WordPress的插件,当WordPress中的内容改变时,将清除varnish中的内容。 它还附带了一个示例VCL应该给你一个更好的命中率

示例VCL

 backend default { .host = "127.0.0.1"; .port = "8080"; } acl purge { "localhost"; } sub vcl_recv { if (req.request == "PURGE") { if(!client.ip ~ purge) { error 405 "Not allowed."; } return(lookup); } if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") { return (pipe); } if (req.request != "GET" && req.request != "HEAD") { return (pass); } if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") { return (pass); } remove req.http.cookie; return (lookup); } sub vcl_hit { if (req.request == "PURGE") { purge; error 200 "Purged."; } } sub vcl_miss { if (req.request == "PURGE") { purge; error 200 "Purged."; } } sub vcl_fetch { if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") { return (hit_for_pass); } set beresp.ttl = 24h; return (deliver); }