Changing Nginx keep-alive behavior based on error response of proxied server

I was wondering if there is a way in Nginx to force a client to close
the
connection (or modify the keepalive parameters) when a proxied server
returns a particular error response. To elaborate a bit, if I have Nginx
as
a proxy in front of a backend server, can Nginx be made to change its
keep
alive behavior based on the error response of the back end server? For
example, if I have keepalive_requests as, say 30, in my Nginx config but
if
the proxied server returns some 4xx or 5xx error, I’d like to send a
connection close to the client or perhaps make keepalive_requests as 0
for
that connection, forcing the client to open up a new connection.

One approach I tried was to intercept the error (proxy_intercept_errors
on)
and used the error page directive to refer to a location wherein I set
the
keepalive_requests as 0. This seems to make the client close the current
connection but there doesn’t seem to be a way to return the actual
response
from the backend server when using proxy_intercept_errors. It would be
nice
to be able to return the actual error response from the backend server
than
just return some static content.
Please let me know if anyone has any suggestions or ideas.
Thanks.

Posted at Nginx Forum:

Any ideas/suggestions related to forcing a client connection close on
certain errors from proxied server? Please refer my previous post for
details.
Thanks.

Posted at Nginx Forum: