Caching Modified URLs by Varnish instead of the original requested URL

Geoff Simmons geoff at uplex.de
Fri Sep 1 13:19:09 UTC 2023


On 8/31/23 22:06, Guillaume Quintard wrote:
> 
> Other options include vmod_querystring 
> <https://github.com/Dridi/libvmod-querystring/blob/master/src/vmod_querystring.vcc.in>
> (Dridi might possibly be of assistance on this topic) and
> vmod_urlplus
> <https://docs.varnish-software.com/varnish-enterprise/vmods/urlplus/#query_get>
> (Varnish Enterprise), and the last, and possibly most promising one,
> vmod_re2
> <https://gitlab.com/uplex/varnish/libvmod-re2/-/blob/master/README.md>

I would suggest going with vmod_re for a task like this:

https://gitlab.com/uplex/varnish/libvmod-re

Because:

- VMOD re is based on Varnish's internal interface for regex-ing, so it 
uses the pcre2 library that's always installed with Varnish. For VMOD 
re2 you also have to install the re2 library.

- pcre2 regex matching is generally faster than re2 matching. The point 
of re2 regexen is that matches won't go into catastrophic backtracking 
on pathological cases.

- The real strength of re2 lies in the set interface, which matches 
multiple regexen "simultaneously", and then can tell you which one 
matched. The matching regex can be associated with a backend, a 
subroutine, or a number of other VCL objects; and there are a variety of 
other bells and whistles. VMOD re is just about subexpression capture, 
which is the job to be done here.

For either VMOD re or re2, it's a good idea to initialize the regex in 
vcl_init, so that it's pre-compiled at runtime. The versions of the 
match function that take a regex as a parameter compile the regex on 
every invocation.

So with VMOD re it would look like this:

import re;

sub vcl_init {
	new query_pattern = re.regex(".*(q=)(.*?)(\&|$).*");
}

sub vcl_recv {
	if (query_pattern.match(req.url)) {
		set req.http.hash-url = query_pattern.backref(1) +
			std.lower(query_pattern.backref(2)) +
			query_pattern.backref(3);
	}
}


HTH,
Geoff
-- 
** * * UPLEX - Nils Goroll Systemoptimierung

Scheffelstraße 32
22301 Hamburg

Tel +49 40 2880 5731
Mob +49 176 636 90917
Fax +49 40 42949753

http://uplex.de

-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20230901/ad72de7e/attachment.bin>


More information about the varnish-misc mailing list