How to ignore specific query parameter with varnish cache?

Tanel Tammik

/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.

Helmut Januschka

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to keep specific query parameters in varnish

Varnish not to cache urls with specific word

RTK Query customize and ignore specific query params used to cache requests

How to ignore big cookies in varnish

How to clear complete cache in Varnish?

How to bypass varnish cache on client?

How to cache based on size in Varnish?

How to cache an object on Varnish, but tell the client not the cache it

How to ignore a parameter in a prepared mysqli query in PHP?

How to remove files from Varnish-cache

How to make Cache Tagging work with FOSHttpCacheBundle and Varnish?

How to use Varnish HTTP-Cache in Laradock?

How to map apache authgroupfile with varnish-cache

Varnish how to cache for mobile and desktop site

How pass ignore_unavailable as query parameter to elasticsearch search request?

How to add 'state' query parameter to OAuth request (Or ignore it)

How to ignore "cannot resolve query parameter" error in IntelliJ

how to ignore the immediate parent which is of specific type in hierarchical query

Mongoose: ignore parameter in query if it is null

How to cache an asset when the URL of the asset has a variable query parameter?

How to reset cache from only one specific query

APIM Cache base on query parameter

rewrite specific query parameter

How To Force Varnish Cache to Keep All Cached Pages Indefinitely?

How to instruct varnish to generate the cache key based on response header data

How to disable Varnish Cache from within a PHP script?

How to run varnish cache without IPv6?

How to (varnish) cache webp images based on accept header

Varnish: how to cache a URL with parameters - need to strip multiple parameters but not all