# When does nginx return a Bad gateway (502)?

**URL:** <https://www.ruby-forum.com/t/when-does-nginx-return-a-bad-gateway-502/227572>\
**Category:** NGINX\
**Created:** [March 19, 2013, 9:29pm UTC](https://www.ruby-forum.com/t/when-does-nginx-return-a-bad-gateway-502/227572 "2013-03-19T21:29:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fastcatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/db5fbb/32.png) [@fastcatch](https://www.ruby-forum.com/u/fastcatch)\
**Post date:** [March 19, 2013, 9:29pm UTC](https://www.ruby-forum.com/t/when-does-nginx-return-a-bad-gateway-502/227572/1 "2013-03-19T21:29:35Z")

</div>

I have an application behind nginx (Rails on Unicorn if it matters)  
which  
listens on a UNIX socket.

## It all works nice, especially when load is low. When there is some load on the server though (say 70%), I randomly(?) get a bunch of 502 responses

usually in batches.

Thus the question: when (under what conditions) does nginx send a 502  
response?

Thanks in advance,

fastcatch

PS: The app’s worker processes do not seem to be irresponsive, slow,  
blocked  
for IO or anything else I have been able to identify as a possible  
cause.  
Thus I want to understand more the nginx side so as to find the root  
cause  
– which I believe is on the other side (or some config tweak needed  
soemwhere).

Posted at Nginx Forum:

> **[When does nginx return a Bad gateway (502)?](https://forum.nginx.org/read.php?2,237573,237573#msg-237573)**
>
> I have an application behind nginx (Rails on Unicorn if it matters) which listens on a UNIX socket. It all works nice, especially when load is low. When there is some load on the server though (say 70%), I randomly(?) get a bunch of 502 responses --...

---

<div class="post-metadata">

**Author:** ![fastcatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/db5fbb/32.png) [@fastcatch](https://www.ruby-forum.com/u/fastcatch)\
**Post date:** [March 19, 2013, 9:48pm UTC](https://www.ruby-forum.com/t/when-does-nginx-return-a-bad-gateway-502/227572/2 "2013-03-19T21:48:16Z")

</div>

In my experience, Nginx returns 502 when the upstream server (Unicorn in  
your case) doesn’t respond or terminates the connection unexpectedly.

\_Nik

---

<div class="post-metadata">

**Author:** ![fastcatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/db5fbb/32.png) [@fastcatch](https://www.ruby-forum.com/u/fastcatch)\
**Post date:** [March 20, 2013, 11:04am UTC](https://www.ruby-forum.com/t/when-does-nginx-return-a-bad-gateway-502/227572/3 "2013-03-20T11:04:02Z")

</div>

Thank you.

BTW: having googled it for different expressions I’ve found that it is a  
well known issue and that it has nothing to do with nginx or the Rails  
app  
itself. Unicorn in and of itself can neither be blamed (albeit IMHO  
there  
should be some official documentation on this issue, esp. since nginx is  
the  
preferred/“official” web server for unicorn). It looks like a socket  
backlog setting issue to be handled at OS level but I’m still unsure if  
that’s a resolution or not.

Posted at Nginx Forum:

> **[Re: When does nginx return a Bad gateway (502)?](https://forum.nginx.org/read.php?2,237573,237590#msg-237590)**
>
> I have an application behind nginx (Rails on Unicorn if it matters) which listens on a UNIX socket. It all works nice, especially when load is low. When there is some load on the server though (say 70%), I randomly(?) get a bunch of 502 responses --...

---

<div class="post-metadata">

**Author:** ![fastcatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/db5fbb/32.png) [@fastcatch](https://www.ruby-forum.com/u/fastcatch)\
**Post date:** [March 21, 2013, 11:03am UTC](https://www.ruby-forum.com/t/when-does-nginx-return-a-bad-gateway-502/227572/4 "2013-03-21T11:03:51Z")

</div>

I can confirm that allowing a longer backlog queue (in unicorn config +  
net.core.somaxconn and net.core.netdev\_max\_backlog) did solve the issue  
(for  
me).

I posted a bit more details at  
[Bad gateway errors at load on nginx + Unicorn (Rails 3 app) - Stack Overflow](http://stackoverflow.com/a/15544229/1061997).

Posted at Nginx Forum:

> **[Re: When does nginx return a Bad gateway (502)?](https://forum.nginx.org/read.php?2,237573,237622#msg-237622)**
>
> I have an application behind nginx (Rails on Unicorn if it matters) which listens on a UNIX socket. It all works nice, especially when load is low. When there is some load on the server though (say 70%), I randomly(?) get a bunch of 502 responses --...
