3

/swatches/ajax/media/?product_id=17620&isAjax=true&_=1524469655019

I need to cache this request with varnish, but i need to ignore the last query parameter _=1524469655019.

I am not sure how varnish internally works, but i suppose it makes the cache uid key from the url requested. So in my case it would need it only to create the uid key from this url

/swatches/ajax/media/?product_id=17620&isAjax=true

Doing something like req.url ~ "^/swatches/(.*)$" would not work as varnish would still use the entire url for cache uid.

Tanel Tammik
  • 15,489
  • 3
  • 22
  • 31

1 Answers1

1

just strip the _= from the QS.

sub vcl_recv {
  set req.url = regsuball(req.url,"\?_=[^&]+$",""); # strips when QS = "?_=AAA"
  set req.url = regsuball(req.url,"\?_=[^&]+&","?"); # strips when QS = "?_=AAA&foo=bar"

}

keep in mind this will strip the QS - and the QS is not forwarded to the backend. if you need it in the backend you'd need to do in in vcl_hash

beware if you have a custom vcl_hash, to include the default rules, as the custom removes the default hash parameters. http.host || client.ip

VCL hash is the part the generates the cache-uuid

sub vcl_hash {
  set req.http.hash_url = regsuball(req.url,"\?_=[^&]+$",""); # strips when QS = "?_=AAA"
  set req.http.hash_url = regsuball(req.http.hash_url,"\?_=[^&]+&","?"); # strips when QS = "?_=AAA&foo=bar"
  hash_data(req.http.hash_url);
  if (req.http.host) {
    hash_data(req.http.host);
  } else {
    hash_data(server.ip);
  }
}    

this way, the request that produces a MISS also passes _= to the backend. and there is no explicit need for a vmod in such a simple case.

Community
  • 1
  • 1
Helmut Januschka
  • 1,578
  • 3
  • 16
  • 34