Need Assistance in Configuring Varnish to Retain and Ignore Unique Parameter in Request URL while caching

Guillaume Quintard guillaume.quintard at gmail.com
Tue May 30 14:49:13 UTC 2023


Hi Uday,

Ultimately, you'll probably want to learn and use this vmod:
https://github.com/Dridi/libvmod-querystring , but in the meantime, we can
use a quick hack.

Essentially, we don't need to modify the URL, but we can just alter the
cache key computation.

By default, the key logic looks like this:

sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    return (lookup);
}

(It's done by default if you don't have a vcl_hash section in your VCL)
We can tweak it slightly so that we ignore the whole querystring:

sub vcl_hash {
    hash_data(regsub(req.url, "\?.*",""));
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    return (lookup);
}

It's crude, but should do the job. To use it, just copy the code above in
your VCL, for example just after the vcl_recv definition (not inside it
though). Of course, if you already have a vlc_hash definition in your code,
you'll need to modify that one, instead of adding a new one.

Relevant documentation:
- https://varnish-cache.org/docs/trunk/users-guide/vcl-hashing.html
- https://www.varnish-software.com/developers/tutorials/varnish-builtin-vcl/

Last note: it would probably be better if the tomcat server didn't need
that unique parameter, or at the very least, if Varnish could just add it
itself rather than relying on client information as you're caching
something public using something that was user-specific, so there's
potential for snafus here.

Hope that helps,


On Tue, May 30, 2023, 03:45 Uday Kumar <uday.polu at indiamart.com> wrote:

> Hello everyone,
>
> In our system, we're currently using Varnish Cache in front of our Tomcat
> Server for caching content.
>
> As part of our new requirement, we've started passing a unique parameter
> with every URL. The addition of this unique parameter in each request is
> causing a cache miss, as Varnish treats each request as distinct due to the
> difference in the parameter. Our intent is to have Varnish ignore this
> specific parameter for caching purposes, so that it can treat similar
> requests with different unique parameters as identical for caching purposes.
>
> Expected Functionality of Varnish:
>
> 1. We need Varnish to ignore the unique parameter when determining if a
> request is in the cache or while caching a request.
>
> 2. We also need Varnish to retain this unique parameter in the request
> URL when it's passed along to the Tomcat Server.
>
> We're looking for a way to modify our Varnish configuration to address the
> above issues, your assistance would be greatly appreciated.
>
>
> Thanks & Regards
> Uday Kumar
> _______________________________________________
> varnish-misc mailing list
> varnish-misc at varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20230530/3049183c/attachment-0001.html>


More information about the varnish-misc mailing list