Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

    ...
}

There's a good couple of quick tools to test publicly if varnish is caching - https://isvarnishworking.uk and https://isvarnishworking.co.uk that I use to test if varnish is working on a couple of sites. They rely on the X-Varnish and X-Cache headers mainly but show all the headers so you can set whatever you want in your varnish vcl and you can see the headers on the webpages

To verify that Varnish is proxying look for the existence of the X-Varnish header in the response. The Age header will be 0 on a cache miss and above zero on a hit. The first request to a page will always be a miss.

curl with the -v flag can be used to show the headers. Using curl from bash:

curl -v http://localhost:8080

Example output of a cache hit:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/x.xx.x
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8
< Server: nginx/1.20.0
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Content-Length: 45703
< X-Varnish: 32770 3
< Age: 6
< Via: 1.1 varnish (Varnish/7.0)
< Accept-Ranges: bytes
< Connection: keep-alive
<
<!DOCTYPE html>
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top