How to check if Varnish cache is working correctly?

LittleLebowski picture LittleLebowski · Dec 23, 2013 · Viewed 26.3k times · Source

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.

Answer

juneih picture juneih · Dec 29, 2013

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.