Need Assistance in Configuring Varnish to Retain and Ignore Unique Parameter in Request URL while caching
Guillaume Quintard
guillaume.quintard at gmail.com
Mon Jun 5 14:28:02 UTC 2023
Hi,
Relevant documentation:
- https://varnish-cache.org/docs/trunk/users-guide/vcl-hashing.html
- https://www.varnish-software.com/developers/tutorials/varnish-builtin-vcl/
- https://varnish-cache.org/docs/trunk/users-guide/vcl-built-in-code.html
Essentially: if you don't use a return statement, then the built-in vcl
code is executed, and so the logic will be different with and without that
statement.
You wrote that the code isn't working, but don't explain further, which
makes it hard to debug, my best guess is that you're hashing too much
because of the built-in code.
One thing you can do is this:
```
sub vcl_deliver {
set resp.http.req-hash = req.hash;
...
}
```
That will allow you to see objects get the same hash, or a different one.
On that topic, I'm pretty certain that hashing the Accept-Encoding header
is useless and will fragment your cache needlessly, as Varnish already
takes that header into account implicitly.
Note that the vcl I shared in my last email doesn't have a vcl_hash
function because it relies entirely on modifying the url before it is
hashed by the built-in vcl.
Hope that helps.
--
Guillaume Quintard
On Mon, Jun 5, 2023 at 4:31 AM Uday Kumar <uday.polu at indiamart.com> wrote:
> Hello Guillaume,
>
> Thanks for the update!
>
>
> (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);
>>>>> }
>>>>>
>>>>
> Would like to discuss about above suggestion.
>
> *FYI:*
> *In our current vcl_hash subroutine, we didnt had any return lookup
> statement in production , and the code is as below*
> #Working
> sub vcl_hash{
> hash_data(req.url);
> hash_data(req.http.Accept-Encoding);
> }
> The above code is *working without any issues on production even without
> return (lookup)* statement.
>
> For our new requirement * to ignore the parameter in URL while caching, * as
> per your suggestion we have made changes to the vcl_hash subroutine, new
> code is as below.
>
> #Not Working
> sub vcl_hash{
> set req.http.hash-url = regsuball(req.url, "traceId=.*?(\&|$)", "");
> hash_data(req.http.hash-url);
> unset req.http.hash-url;
> hash_data(req.http.Accept-Encoding);
> }
>
> The above code is *not hashing the URL by ignoring traceId (not as
> expected)** but if I add return lookup at the end of subroutine its
> working as expected.*
>
> #Working Code
> sub vcl_hash{
> set req.http.hash-url = regsuball(req.url, "traceId=.*?(\&|$)", "");
> hash_data(req.http.hash-url);
> unset req.http.hash-url;
> hash_data(req.http.Accept-Encoding);
> *return (lookup);*
> }
>
>
> *I have few doubts to be clarified:*
> 1. May I know what difference return (lookup) statement makes?
> 2. Will there be any side effects with modified code, if I use return
> (lookup)? (Because original code was not causing any issue even without
> return lookup in production)
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20230605/6a884fe3/attachment.html>
More information about the varnish-misc
mailing list