NGINX proxy caching - cache buster variable in querystring - possible to ignore?

5,923

We have found a solution to this question.

The correct method of doing this is to construct a cache key by using nginx $arg_... variables. $arg_... will contain query string parameters. So in the above example we would use $arg_parameter and $arg_parameter2 within the cache key.

The result in the nginx.conf looks like:

http {
  ...
    server {
      location / {
         ...
         proxy_cache_key $scheme$proxy_host$uri$is_args$arg_parameter$arg_parameter2;
      }
    }
  ...
}

Note that querystring parameter containing an hyphen (dash), such as data-* parameters, do not work with the $arg_paramName syntax. A workaround using Lua can be found in this post.

Share:
5,923

Related videos on Youtube

محمّد محسن احمدی
Author by

محمّد محسن احمدی

Updated on September 18, 2022

Comments

  • محمّد محسن احمدی
    محمّد محسن احمدی almost 2 years

    We have the following url we would like to proxy cache:

    file.php?parameter=one&paramater2=two&r=EPOCHTIMESTAMP
    

    Query string parameter "parameter" varies between requests. So does "paramater2".

    Query stringing parameter r is a timestamp we use to make sure the client doesn't serve cached (on the client side) content. Aka "cache buster". Yes we also use all the appropriate don't cache h headers.

    Now, we would like to proxy cache via nginx some of these requests. Is it possible to instruct nginx to ignore the r querystring param but use all the others when setting a cache key for the entry? If we can't ignore param r then the nginx proxy cache will be useless as each cache key will be unique.

    Thanks.