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

**URL:** <https://www.ruby-forum.com/t/changing-nginx-keep-alive-behavior-based-on-error-response-of-proxied-server/236117>\
**Category:** NGINX\
**Created:** [August 6, 2013, 9:25pm UTC](https://www.ruby-forum.com/t/changing-nginx-keep-alive-behavior-based-on-error-response-of-proxied-server/236117 "2013-08-06T21:25:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [August 6, 2013, 9:25pm UTC](https://www.ruby-forum.com/t/changing-nginx-keep-alive-behavior-based-on-error-response-of-proxied-server/236117/1 "2013-08-06T21:25:43Z")

</div>

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:

> **[Changing Nginx keep-alive behavior based on error response of proxied server](https://forum.nginx.org/read.php?2,241653,241653#msg-241653)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![nginxCoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nginxCoder](https://www.ruby-forum.com/u/nginxCoder)\
**Post date:** [August 9, 2013, 5:36am UTC](https://www.ruby-forum.com/t/changing-nginx-keep-alive-behavior-based-on-error-response-of-proxied-server/236117/2 "2013-08-09T05:36:15Z")

</div>

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:

> **[Re: Changing Nginx keep-alive behavior based on error response of proxied server](https://forum.nginx.org/read.php?2,241653,241721#msg-241721)**
>
> 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...
