Question

I would like to avoid cache on same page depending of a cookie value (my user have profile with an URL like /firstname-lastname and can visit profile of other users).

In my cookies I store the "firstname-lastname" value, and try to make a test on it. So I have made this :

set req.http.X-SLUG = regsub(req.http.cookie, ".*user_slug=([^;]+);.*", "\1");
if (req.url ~ "^/" + req.http.X-SLUG) {

And here the compilation error :

Expected ')' got '+' (program line 73), at ('conf.vcl' Line 50 Pos 28)
         if (req.url ~ "^/" + req.http.X-SLUG) {
 ---------------------------#-------------------

Do you knon how to do this?

Thanks

Was it helpful?

Solution

In varnish, all regexes are compiled when the configurations are first loaded, and therefore must be static strings.

One way to work around this limitation is to use regsub call to extract the portion of the URL you are interested in, then test for equality:

set req.http.X-SLUG = regsub(req.http.cookie, ".*user_slug=([^;]+).*", "\1");
set req.http.X-TMP-URL = regsub(req.url, "^/(.*)$", "\1");
if (req.http.X-SLUG == req.http.X-TMP-URL) {
    ....
}
remove req.http.X-TMP-URL;

Note, in the above example I removed a ; from the X-SLUG regex. This allows the regex to work even when user_slug is the last cookie in the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top