Logging hits only with varnishncsa

varnish

By default, varnishncsa logs both hits and misses. As I'm already logging misses in my back-end server logs, I'm looking to only log hits in varnishncsa to remove duplication and keep logfile size down. Has anyone managed to do this?

I'm running Varnish 3.0.2 on Ubuntu 12.04 Server x64.

Best Answer

On my varnish instances I include an 'X-Cache' header that says HIT or MISS as appropriate. With that you can just look for that in 'TxHeader':

varnishncsa -c -m "TxHeader:X-Cache: HIT"

To add this header you can add something like this in your .vcl file:

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