Search code examples
varnishvarnish-vcl

Varnish 6 missing requests for same URL coming from different browsers


This is how my varnish.vcl looks like.

vcl 4.0;

import directors;
import std;

backend client {
    .host = "service1";
    .port = "80";
}

sub vcl_recv {

    std.log("varnish log info:" + req.http.host);

    # caching pages in client

    set req.backend_hint = client;

    # If request is from conent or for pages remove headers and cache
    if ((req.url ~ "/content/") || (req.url ~ "/cms/api/") || req.url ~ "\.(png|gif|jpg|jpeg|json|ico)$" || (req.url ~ "/_nuxt/") ) {
        unset req.http.Cookie;
        std.log("Cachable request");
    }
    # If request is not from above do not cache and pass to Backend.
    else
    {
        std.log("Non cachable request");
        return (pass);
    }
}


sub vcl_backend_response {
    if ((bereq.url ~ "/content/") || (bereq.url ~ "/cms/api/") || bereq.url ~ "\.(png|gif|jpg|jpeg|json|ico)$" || (bereq.url ~ "/_nuxt/") )
    {
        unset beresp.http.set-cookie;
        set beresp.http.cache-control = "public, max-age=259200";
        set beresp.ttl = 12h;
        return (deliver);
    }

}

# Add some debug info headers when delivering the content:
# X-Cache: if content was served from Varnish or not
# X-Cache-Hits: Number of times the cached page was served
sub vcl_deliver {

        # Was a HIT or a MISS?
        if ( obj.hits > 0 )
        {
                set resp.http.X-Cache-Varnish = "HIT";
        }
        else
        {
                set resp.http.X-Cache-Varnish = "MISS";
        }

        # And add the number of hits in the header:
        set resp.http.X-Cache-Hits = obj.hits;
}

If I am hitting a page from same browser netwrok tab showing

X-Cache-Varnish = "HIT";
X-Cache-Hits = ;

Lets say if I hot from chrome 10 times this is what I get

X-Cache-Varnish = "HIT";
X-Cache-Hits = 9;

9 because first was a miss and rest 9 were served from cache.

If I try incognito window or a different browser it gets its own count starting from 0. I think somehow I am still caching cookies. I could not identify what I am missing.

Ideally, I want to delete all cookies for specific paths. but somehow unset does not seem to be working for me.


Solution

  • If you really want to make sure these requests are cached, make sure you do a return(hash); in your if-statement.

    If you don't return, the built-in VCL will take over, and continue executing its standard behavior.

    Apart from that, it's unclear whether or not your backend sets a Vary header which might affect your hit rate.

    Instead of guessing, I suggest we use the logs to figure out it.

    Run the following command to track your requests:

    varnishlog -g request -q "ReqUrl ~ '^/content/'"
    

    This statement's VSL Query expression assumes the URL starts with /content. Please adjust accordingly.

    Please send me an extract of varnishlog for 1 specific URL, but also for both situations:

    • The one that hits the cache on a regular browser tab
    • The one that results in a cache miss in incognito mode or from a different browser

    The logs will give more context and explain what happened.