Question

I'm using the following vcl to help me determine if I'm getting a Varnish cache hit or a miss:

sub vcl_deliver {
  if (obj.hits > 0) {
    set resp.http.X-Varnish-Cache = "HIT";
  }
  else {
    set resp.http.X-Varnish-Cache = "MISS";
  }
}

This is working fine, but not for application/json content type. I'm getting X-Varnish-Cache = "MISS" on every request, but I can't see why this would be happening (and I have a suspicion that it's a false negative).

Here's an example of the response headers for my (apparently) non cached json request:

Accept-Ranges:bytes
Age:0
Cache-Control:public, max-age=10800
Connection:keep-alive
Content-Encoding:gzip
Content-Length:2139
Content-Type:application/json
Date:Thu, 09 Jan 2014 16:08:21 GMT
Etag:"1389283199-1"
Expires:Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified:Thu, 09 Jan 2014 15:59:59 +0000
Server:Apache
Vary:Cookie,Accept-Encoding
Via:1.1 varnish
X-Drupal-Cache:HIT
X-Powered-By:PHP/5.3.23
X-Varnish:544296535
X-Varnish-Cache:MISS

If I look at the response times, it's 8 times quicker on subsequent requests to the same url (so 800ms for the first time, and 100ms the second and then on). This makes me think that Varnish is caching, but my vcl is incorrect.

I should also add that no cookies are being sent with the request, and it's a GET request.

Was it helpful?

Solution

Your VCL is correct (BTW: when varnish response comes from cache X-Varnish header comes with 2 numbers).

The response times improves due to Drupal caching.

Probably JSON is not being cached because of the cookies or your VCL.

You have a working VCl for drupal on https://github.com/NITEMAN/varnish-bites/blob/master/varnish3/drupal-base.vcl

Anyway, to trace the issue your full VCL and a varnishlog trace of the request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top