How to check if Varnish cache is working correctly?

Viewed 36386

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.

4 Answers

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>
...
Related