Nginx error upstream prematurely closed connection while reading response header from upstream, client:
<p>I am using NGINX as a load balancer on my server which just proxies the request to application servers according to their weight.</p>

<p>Since this morning I have started getting this error on browser </p>

<blockquote>
  <p>502 Bad Gateway</p>
</blockquote>

<p>and checking the NGINX error logs it says</p>

<blockquote>
  <p>upstream prematurely closed connection while reading response header from upstream, client:</p>
</blockquote>

<p>On the application server (Apache + PHP) page called invokes a PayPal setexpresscheckout request through php-sdk.</p>

<p>Sometimes it works (refreshing page multiple times) without throwing 502, so the problem is intermittent.</p>

<p>I have tried following options:</p>

<ol>
<li>Increasing proxy buffer size in NGINX conf.</li>
<li>Turning buffering option off</li>
<li>Increasing proxy read timeout up to 5s</li>
</ol>

<p>None of the above worked.  Can anyone please point me in right direction?</p>