Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Varnish cache is working correctly?

Tags:

varnish

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.

like image 915
LittleLebowski Avatar asked Dec 23 '13 16:12

LittleLebowski


2 Answers

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.

like image 146
juneih Avatar answered Oct 11 '22 19:10

juneih


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";
    }

    ...
}
like image 34
Ray Jennings Avatar answered Oct 11 '22 20:10

Ray Jennings