Score:0

NGINX: Can I to use proxy_no_cache directive with upstream response body for conditional caching?

cn flag

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

Score:0
us flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.