Skip to content

Magento 2 Configured Varnish after check X-Magento-Cache-Debug show Miss?

i have configured varnish in magento 2.4.5. i am getting varnishlog command X-Magento-Cache-Debug: MISS. currently i am using apache server.

I have configure varnish in Magento 2.4.5. Backend configured as per the below Screenshot.
https://i.imgur.com/j0Oj2UL.png

but in frontend not show “X-Magento-Cache-Debug:MISS/HIT”.

https://i.imgur.com/6QMBU1o.png

i have checked in server log using the below command.i am getting the below response and get

X-Magento-Cache-Debug : MISS

sudo varnishlog

*   << Request  >> 66232
-   Begin          req 66231 rxreq
-   Timestamp      Start: 1685699104.524062 0.000000 0.000000
-   Timestamp      Req: 1685699104.524062 0.000000 0.000000
-   VCL_use        boot
-   ReqStart       127.0.0.1 51720 a0
-   ReqMethod      GET
-   ReqURL         /health_check.php
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: localhost
-   ReqHeader      Connection: close
-   ReqHeader      X-Forwarded-For: 127.0.0.1
-   VCL_call       RECV
-   VCL_return     pass
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       PASS
-   VCL_return     fetch
-   Link           bereq 66233 pass
-   Timestamp      Fetch: 1685699104.524305 0.000242 0.000242
-   RespProtocol   HTTP/1.1
-   RespStatus     503
-   RespReason     Backend fetch failed
-   RespHeader     Date: Fri, 02 Jun 2023 09:45:04 GMT
-   RespHeader     Server: Varnish
-   RespHeader     content-type: text/html; charset=utf-8
-   RespHeader     Retry-After: 5
-   RespHeader     X-Varnish: 66232
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.6)
-   VCL_call       DELIVER
-   RespHeader     **X-Magento-Cache-Debug: MISS**
-   RespHeader     Pragma: no-cache
-   RespHeader     Expires: -1
-   RespHeader     Cache-Control: no-store, no-cache, must-revalidate, max-age=0
-   RespUnset      Age: 0
-   RespUnset      Server: Varnish
-   RespUnset      X-Varnish: 66232
-   RespUnset      Via: 1.1 varnish (Varnish/6.6)
-   VCL_return     deliver
-   Timestamp      Process: 1685699104.524348 0.000285 0.000043
-   Filters
-   RespHeader     Content-Length: 282
-   RespHeader     Connection: close
-   Timestamp      Resp: 1685699104.524431 0.000368 0.000082
-   ReqAcct        70 0 70 293 282 575
-   End

I have debug it /etc/varnish/default.vcl file and check “sub vcl_deliver”. if condition resp.http.x-varnish not true that’s why it will not Hit Varnish.

sub vcl_deliver {
    
    if (resp.http.x-varnish ~ " ") {
        set resp.http.X-Magento-Cache-Debug = "HIT";
        set resp.http.Grace = req.http.grace;
    } else {
        set resp.http.X-Magento-Cache-Debug = "MISS";
    }

    # Not letting browser to cache non-static files.
    if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
        set resp.http.Pragma = "no-cache";
        set resp.http.Expires = "-1";
        set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
    }

    if (!resp.http.X-Magento-Debug) {
        unset resp.http.Age;
    }
    unset resp.http.X-Magento-Debug;
    unset resp.http.X-Magento-Tags;
    unset resp.http.X-Powered-By;
    unset resp.http.Server;
    unset resp.http.X-Varnish;
    unset resp.http.Via;
    unset resp.http.Link;
}