Cache-control: Is it possible to ignore query para

2019-07-19 18:07发布

问题:

Is it possible to set a cache-control header communicating with a reverse proxy to ignore query parameters in determining what is a unique uri or in short: validate a cache even if some query parameters have changed?

Sometimes query parameters have nothing to do with the rendering of the page at least from a server side perspective. For instance all utm_* variables from Google Adwords. These are needed for the javascript on your page so you don't want to strip them away and redirect to a cached page but at the same time it would be advantageous not to treat two uri's which are basically the same but have different utm_* parameters as unique when communicating with a reverse proxy.

An example:

  1. http://www.example.com/search?sort=price

  2. http://www.example.com/search?sort=price&utm_campaign=shoes

Is there anyway to tell the reverse proxy using the HTTP 1.1 spec (i.e. some type of http header) that it can just treat these two pages as the same?

回答1:

You can filter the query string in vcl_recv and there is also a Varnish module for that [1].

Also, you have to keep in mind that query string parameter order matters in this case [2]

See also this related question [3]

[1] https://www.varnish-cache.org/vmod/querystring

[2] http://cyberroadie.wordpress.com/2012/01/05/varnish-reordering-query-string/

[3] Stripping out select querystring attribute/value pairs so varnish will not vary cache by them