How to cache image urls with query strings?

jeanpaul nginx-forum at forum.nginx.org
Sun Feb 26 10:12:53 UTC 2017


Hi,

The proxy_cache_key uses request parameters by default. As stated in
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key it
uses $scheme$proxy_host$request_uri by default. 

The $request_uti do contain the request parameters:
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri

$request_uri
full original request URI (with arguments)

So a way to deal with this is using a self-made cache_key and strip the
arguments with a regex:

set $cacheuri $request_uri;
if ($cacheuri ~ /example/images/([^\?]*)) {
     set $cacheuri /example/images/$1;
}
proxy_cache_key $cacheuri;

Note: the regex is untested, but just to give you an idea.

JP

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,272608,272623#msg-272623



More information about the nginx mailing list