Score:0

Modify nginx proxy_pass response

ph flag

I have an nginx that proxies requests to a couple of third party APIs (using proxy_pass), for caching purposes. One of the APIs is always returning 200 responses with the error status code buried inside the JSON response body. This causes nginx to cache the error responses, which is not desired.

Question is: is there a way to intercept the response from the API from the proxy_pass, extract the error code and set it as the HTTP response status code?

I tried to use the perl and njs modules, but I'm starting to think this is not possible, am I missing something? or maybe there is another way I haven't considered.

Thank you.

ws flag
While it is possible to do this with (e.g.) the lua module (I suspect other embedded scripting may work to - I am not familiar with perl or njs in this regard) that is not the right way to solve the problem. Instead of proxying the API from nginx, do this via a separate component either using proxying or fastcgi.
Dan F. avatar
ph flag
@symcbean by a "separate component" you mean sending the request to a backend (ex: nodejs, php) that is gonna make the API call?
ws flag
yes - you get to use the tool you're most familiar with and properly layering your application components as well as it just being lot simpler.
I sit in a Tesla and translated this thread with Ai:

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.