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.
How to know for sure? Can someone please guide me. I'm new to Varnish cache.
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 curl or GET. Here's a GET example:
sudo apt-get install libwww-perl && GET -Used http://localhost:6081/index.html
The two headers to look for are X-Varnish and Age. X-Varnish will 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: 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:
X-Varnish: 107856170 107856168
The Age header says how many seconds old the cached copy is. With a miss it will be 0, and with a hit it's > 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.
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.
For example, in 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";
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With