wordpress 如何检查 Varnish 缓存是否正常工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20747210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if Varnish cache is working correctly?
提问by LittleLebowski
I'm using Varnish Cache on a Wordpress website that runs on Nginx. It's configured the way mentioned in this blog. It's working, but I'm not sure if it's actually serving content from the cache.
我在 Nginx 上运行的 Wordpress 网站上使用 Varnish Cache。它是按照本博客中提到的方式配置的。它正在工作,但我不确定它是否真的从缓存中提供内容。
How to know for sure? Can someone please guide me. I'm new to Varnish cache.
怎样才能确定?有人可以指导我。我是 Varnish 缓存的新手。
回答by juneih
Varnish will by default add headers to the response of any request it handles. You can look at reponse headers by using browser tools like Firebug, or CLI tools like curlor GET. Here's a GET example:
默认情况下,Varnish 会在它处理的任何请求的响应中添加标头。您可以使用 Firebug 等浏览器工具或curl或GET等 CLI 工具查看响应标头。这是一个 GET 示例:
sudo apt-get install libwww-perl && GET -Used http://localhost:6081/index.html
The two headers to look for are X-Varnishand Age. X-Varnishwill contain one or two numbers in it, the numbers themselves aren't important, but they refer to requests. If a request results in a miss, Varnish fetches the page from the backend and the X-Varnish header in the response contains one number for the current request:
要查找的两个标题是X-Varnish和Age。X-Varnish将包含一两个数字,数字本身并不重要,但它们指的是请求。如果请求未命中,Varnish 会从后端获取页面,并且响应中的 X-Varnish 标头包含当前请求的一个数字:
X-Varnish: 107856168
The next time the same page is requested, it may result in a hit. If it does, Varnish fetches the page from the cache, and also adds the number from the original request:
下次请求相同的页面时,可能会导致命中。如果是,Varnish 会从缓存中获取页面,并添加原始请求中的编号:
X-Varnish: 107856170 107856168
The Ageheader says how many seconds old the cached copy is. With a miss it will be 0, and with a hit it's > 0.
该年龄头说缓存副本多少秒年纪。如果未命中,它将为 0,如果命中则为 > 0。
Note that the backend can set the age header which makes it look like a false hit, and stacked Varnishes can produce false misses in the X-Varnish header. To be absolutely sure when debugging you can add your own header in the VCL hit and miss functions. See this page for a description https://www.varnish-software.com/static/book/VCL_functions.html. As a newcomer to Varnish the X-Varnish and Age header are most likely all you need.
请注意,后端可以设置年龄标头,使其看起来像是误击,而堆叠的清漆可能会在 X-Varnish 标头中产生误报。为了确保在调试时您可以在 VCL 命中和未命中函数中添加自己的标头。有关说明,请参阅此页面https://www.varnish-software.com/static/book/VCL_functions.html。作为 Varnish 的新手,X-Varnish 和 Age 标题很可能是您所需要的。
回答by Ray Jennings
It would be a good idea to add your own X-headers at various points in your vcl so that you can do unit testing on the various code paths and conditions of your vcl.
在 vcl 的各个点添加您自己的 X-header 是个好主意,这样您就可以对 vcl 的各种代码路径和条件进行单元测试。
For example, in vcl_deliver:
例如,在 vcl_deliver 中:
sub vcl_deliver
{
# Insert Diagnostic header to show Hit or Miss
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
}
else {
set resp.http.X-Cache = "MISS";
}
...
}