NGINX Proxy_No_Cache Directive – Conditional Caching with Upstream Response Body

cachenginx

We are using Ngnix as a cache-proxy in front of a 3rd party service and have the following directive to ensure upstream responses with a status code of 200 are cached…

proxy_cache_valid 200 364d

The problem is the 3rd party service returns a 200 when no results are found for the given search. This is indicated by a JSON parameter in the response body, docs: [].

Is it possible to use the proxy_no_cache, or any other, directive with a regex on the upstream body content so we are not caching these "empty" responses? There are no headers present that indicate an "empty" response either.

TIA

Best Answer

No. nginx does not make response payload available as a variable. This is the minimum needed in order to do something like this.

You could create another application that does the upstream request, parses the response and sets HTTP header that tells nginx if the response should be cached or not. Or it could directly set 404 response for empty responses.