16

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.

Braiam
  • 1
  • 11
  • 47
  • 78
LittleLebowski
  • 7,691
  • 13
  • 47
  • 72
  • 2
    Take a look to response headers, they will tell you if Varnish is working and if a requests came from its cache. – NITEMAN Dec 24 '13 at 01:44
  • 4
    DO NOT USE isvarnishworking.com - it's showing "Nope" most of the time, even when everything is OK. Also, headers shown by wget, wbox or Developer Tools are different from what this website shows. It's a terribly misleading tool. – GDR Sep 18 '14 at 14:51

4 Answers4

24

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.

juneih
  • 256
  • 2
  • 3
7

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

    ...
}
Ray Jennings
  • 336
  • 2
  • 10
  • I tries this code, mine is showing X-Cache MISS in the header. so that means my Varnish is not working? but when I do service varnish stop, then my website is down. so Varnish somehow is working, but not caching. any suggestions? thanks – abc Oct 04 '20 at 20:03
  • If you have only one varnish server, no load balancer, then after one retry you should have "HIT" in the header X-Cache. – Quentin Nov 24 '20 at 18:35
1

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

0

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>
...
Evan Byrne
  • 1,105
  • 1
  • 12
  • 17