Varnish – Resolving ‘Hit for Pass’ Cache Refusal

varnish

   13 StatSess     c 127.0.0.2 48714 0 1 1 0 1 1 687 2573
   24 BackendXID   b 1374630903
   24 TxRequest    b GET
   24 TxURL        b /do_ajax.php?action=get-dom
   24 TxProtocol   b HTTP/1.0
   24 TxHeader     b X-Forwarded-Proto: https
   24 TxHeader     b X-Forwarded-Port: 443
   24 TxHeader     b Host: www.officestocks.com
   24 TxHeader     b Accept-Language: en-US,en;q=0.9
   24 TxHeader     b Accept: text/plain, */*; q=0.01
   24 TxHeader     b User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 15_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Mobile/15E148 Safari/604.1
   24 TxHeader     b Referer: https://www.officestocks.com/
   24 TxHeader     b X-Requested-With: XMLHttpRequest
   24 TxHeader     b X-Remote-IP: 184.89.213.63
   24 TxHeader     b X-Forwarded-For: 184.89.213.63
   24 TxHeader     b Accept-Encoding: gzip
   24 TxHeader     b X-Varnish: 1374630903
   24 RxProtocol   b HTTP/1.1
   24 RxStatus     b 200
   24 RxResponse   b OK
   24 RxHeader     b Date: Fri, 04 Mar 2022 20:20:57 GMT
   24 RxHeader     b Server: Apache/2.2.15 (CentOS)
   24 RxHeader     b X-Robots-Tag: noindex
   24 RxHeader     b X-Content-Type-Options: nosniff
   24 RxHeader     b Expires: Wed, 11 Jan 1984 05:00:00 GMT
   24 RxHeader     b Cache-Control: no-cache, must-revalidate, max-age=0
   24 RxHeader     b X-Frame-Options: SAMEORIGIN
   24 RxHeader     b Referrer-Policy: strict-origin-when-cross-origin
   24 RxHeader     b Vary: Accept-Encoding,User-Agent
   24 RxHeader     b Content-Encoding: gzip
   24 RxHeader     b Connection: close
   24 RxHeader     b Content-Type: text/html; charset=UTF-8
   24 Fetch_Body   b 5(eof) cls 0 mklen 1
   24 Length       b 26
   24 BackendClose b default
   12 SessionOpen  c 127.0.0.2 48702 127.0.0.2:80
   12 ReqStart     c 127.0.0.2 48702 1374630903
   12 RxRequest    c GET
   12 RxURL        c /do_ajax.php?action=get-dom
   12 RxProtocol   c HTTP/1.0
   12 RxHeader     c X-Real-IP: 184.89.213.63
   12 RxHeader     c X-Forwarded-For: 184.89.213.63
   12 RxHeader     c X-Forwarded-Proto: https
   12 RxHeader     c X-Forwarded-Port: 443
   12 RxHeader     c Host: www.officestocks.com
   12 RxHeader     c Connection: close
   12 RxHeader     c Accept-Language: en-US,en;q=0.9
   12 RxHeader     c Accept-Encoding: gzip, deflate, br
   12 RxHeader     c Accept: text/plain, */*; q=0.01
   12 RxHeader     c User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 15_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Mobile/15E148 Safari/604.1
   12 RxHeader     c Referer: https://www.officestocks.com/
   12 RxHeader     c Cache-Control: max-age=1000
   12 RxHeader     c X-Requested-With: XMLHttpRequest
   12 VCL_call     c recv
   12 VCL_acl      c MATCH proxyIps 127.0.0.2
   12 VCL_return   c lookup
   12 VCL_call     c hash
   12 Hash         c /do_ajax.php?action=get-dom
   12 Hash         c www.officestocks.com
   12 Hash         c 443
   12 Hash         c 
   12 VCL_return   c hash
   12 HitPass      c 1374630098
   12 VCL_call     c pass pass
   12 Backend      c 24 default default
   12 TTL          c 1374630903 RFC 0 -1 -1 1646425258 0 1646425257 442645200 0
   12 VCL_call     c fetch
   12 TTL          c 1374630903 VCL -0 120 -1 1646425258 -1
   12 TTL          c 1374630903 VCL 121 120 -1 1646425258 -1
   12 VCL_return   c hit_for_pass
   12 ObjProtocol  c HTTP/1.1
   12 ObjResponse  c OK
   12 ObjHeader    c Date: Fri, 04 Mar 2022 20:20:57 GMT
   12 ObjHeader    c Server: Apache/2.2.15 (CentOS)
   12 ObjHeader    c X-Robots-Tag: noindex
   12 ObjHeader    c X-Content-Type-Options: nosniff
   12 ObjHeader    c Expires: Wed, 11 Jan 1984 05:00:00 GMT
   12 ObjHeader    c Cache-Control: no-cache, must-revalidate, max-age=0
   12 ObjHeader    c X-Frame-Options: SAMEORIGIN
   12 ObjHeader    c Referrer-Policy: strict-origin-when-cross-origin
   12 ObjHeader    c Content-Encoding: gzip
   12 ObjHeader    c Content-Type: text/html; charset=UTF-8
   12 Gzip         c u F - 26 0 80 128 138
   12 VCL_call     c deliver deliver
   12 TxProtocol   c HTTP/1.1
   12 TxStatus     c 200
   12 TxResponse   c OK
   12 TxHeader     c Server: Apache/2.2.15 (CentOS)
   12 TxHeader     c X-Robots-Tag: noindex
   12 TxHeader     c X-Content-Type-Options: nosniff
   12 TxHeader     c Expires: Wed, 11 Jan 1984 05:00:00 GMT
   12 TxHeader     c Cache-Control: no-cache, must-revalidate, max-age=0
   12 TxHeader     c X-Frame-Options: SAMEORIGIN
   12 TxHeader     c Referrer-Policy: strict-origin-when-cross-origin
   12 TxHeader     c Content-Encoding: gzip
   12 TxHeader     c Content-Type: text/html; charset=UTF-8
   12 TxHeader     c Content-Length: 26
   12 TxHeader     c Accept-Ranges: bytes
   12 TxHeader     c Date: Fri, 04 Mar 2022 20:20:58 GMT
   12 TxHeader     c X-Varnish: 1374630903
   12 TxHeader     c Via: 1.1 varnish
   12 TxHeader     c Connection: close
   12 TxHeader     c X-Age: 0
   12 TxHeader     c X-Cache: MISS
   12 TxHeader     c X-Pragma: 
   12 TxHeader     c X-Cache-Control: 
   12 TxHeader     c X-Stock: 
   12 TxHeader     c X-URL: /do_ajax.php?action=get-dom
   12 TxHeader     c X-Auth: 
   12 TxHeader     c X-IP: 
   12 Length       c 26
   12 ReqEnd       c 1374630903 1646425257.592263222 1646425258.176410437 0.000068903 0.584094763 0.000052452
   12 SessionClose c Connection: close

So I was debugging and found varnish was doing a cache lookup, but resulted in a hit-for-pass object being returned. But after reading a bit about it here:
https://stackoverflow.com/questions/12691489/varnish-hit-for-pass-means I saw that it was not caching due to high traffic if I understood correctly, is there a way to force varnish to cache the object?

I see things like these, but I am not sure how to overwrite it in my varnish configs:

   12 TxHeader     c Expires: Wed, 11 Jan 1984 05:00:00 GMT

Just saw this, but it didn't help me aside identifying what the problem might be:

https://stackoverflow.com/questions/35449723/varnish-what-is-causing-hit-for-pass

Best Answer

You backend returns a Cache-Control: no-cache, must-revalidate, max-age=0 header which explicitly tells Varnish not to cache.

My initial advice would be to ensure that the /do_ajax.php?action=get-dom resource returns a Cache-Control header that does allow caching.

If that is not possible, you can always use the following VCL code to enforce caching:

sub vcl_backend_response {
    if(bereq.url == "/do_ajax.php?action=get-dom") {
        unset beresp.http.Expires;
        set beresp.http.Cache-control = "public, max-age=3600";
        set beresp.ttl = 1h;
        return(deliver);
    }
}

IMPORTANT: based on the output of your varnishlog command I can see that you're using an outdated version of Varnish. Can you please ensure you're on Varnish 6 or 7? See https://www.varnish-software.com/developers/downloads/ for download & install instructions.