# "502 Bad Gateway" on first request in a setup with Apache 2.4-servers as upstreams

**URL:** <https://www.ruby-forum.com/t/502-bad-gateway-on-first-request-in-a-setup-with-apache-2-4-servers-as-upstreams/244947>\
**Category:** NGINX\
**Created:** [July 15, 2016, 7:22pm UTC](https://www.ruby-forum.com/t/502-bad-gateway-on-first-request-in-a-setup-with-apache-2-4-servers-as-upstreams/244947 "2016-07-15T19:22:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [July 15, 2016, 7:22pm UTC](https://www.ruby-forum.com/t/502-bad-gateway-on-first-request-in-a-setup-with-apache-2-4-servers-as-upstreams/244947/1 "2016-07-15T19:22:57Z")

</div>

The error I am getting in the logs:

“upstream prematurely closed connection while reading response header  
from  
upstream”

After the first request it works for 10-15 seconds without any problem.  
According to tcpdump in the first request (the failing one) the upstream  
is  
receiving one GET and nothing more.

I found one workaround  
([How to configure nginx to hold the connections and retry if the proxied server returns 502? - Hashnode](https://hashnode.com/post/how-to-configure-nginx-to-hold-the-connections-and-retry-if-the-proxied-server-returns-502-cilngkof700iteq53ratetsry))  
which is adding the same upstream twice and adding the “backup”-option.  
The  
first request then fails but the second in all of my cases succeeds  
(checked  
via logs and tcpdump).

Any ideas (also some comments that could lead me to the solution would  
be  
helpful)?

Posted at Nginx Forum:

> **["502 Bad Gateway" on first request in a setup with Apache 2.4-servers as...](https://forum.nginx.org/read.php?2,268306,268306#msg-268306)**
>
> The error I am getting in the logs: "upstream prematurely closed connection while reading response header from upstream" After the first request it works for 10-15 seconds without any problem. According to tcpdump in the first request (the...
