Question: I suddently get a 502 Bad gateway error and I don't understand why this error appear. This error appear moreover only for 1 single page!!...

The exact error in my Nginx log is :

```
 Upstream prematurely closed FastCGI stdout while reading response header from upstream [..] upstream: "fastcgi://unix:/var/run/php5-fpm.sock:"

```

I tried :

* service nginx restart : NOTHING CHANGE
* service php5-fpm restart : NOTHING CHANGE
* to reboot the server : NOTHING CHANGE
* even to restart the mysql service : NOTHING CHANGE

My /var/log/upstart/php5-fpm.log (only a lot of NOTICES) :

```
Terminating...
exiting, bye-bye!
fpm is running, pid 9887
ready to handle connections
systemd monitor interval set to 10000ms

```

that makes me crazy, any idea ?

Answer: 
Strangely enough, this is what worked for me after chasing down a mysterious 502 Bad Gateway error:

* Change `error_reporting` setting in `php-fpm` ini like this:

`error_reporting = ~E_ALL`
* Then restart `php-fpm` and `nginx`

This worked for me after trying the suggested nginx settings above to no avail. I don't have a verified explanation for this, but it would appear that excessive error reporting can overload the php-fpm process.
I had the error only for one type of requests that had 12 items in the body. May be it was related with a body size, but it worked when it was more (13 and more items) or some less (11 and less).

**upstream prematurely closed FastCGI stdout while reading response header from upstream**

```
2021/12/24 07:14:44 [error] 9#9: *49 upstream prematurely closed FastCGI stdout while reading response header from upstream, client: 172.21.0.1, server: , request: "PUT /v1/MY_PAGE/a976d2e5-afc0-4f6d-8d05-17af7dc73f46 HTTP/1.1", upstream: "fastcgi://172.21.0.3:9000", host: "localhost"

```

I used Docker containers with Nginx and PHP-FPM.

Nginx returned **502 Bad Gateway**.

[![enter image description here](https://i.stack.imgur.com/b3JmH.png)](https://i.stack.imgur.com/b3JmH.png)

PHP-FPM was used XDebug and it made the error.

When I turned on debugging the error left and when I turned off debugging the error returned.

[![enter image description here](https://i.stack.imgur.com/iZkxo.png)](https://i.stack.imgur.com/iZkxo.png)

Solution
--------

To solve the error I disabled XDebug. Also it works when I removed XDebug.

Options to solve the error (one of these):

* **Disable XDebug** (one of these):
	+ Set the `XDEBUG_MODE='off'` environment variable.
	+ Set `xdebug.mode=off` in `xdebug.ini` or another PHP config.
	+ <https://xdebug.org/docs/all_settings#mode>
* **Remove XDebug** from your PHP server and restart PHP-FPM (one of these):
	+ Remove through PECL (works with Docker): `pecl uninstall xdebug`.
	+ Remove through Ubuntu APT: `sudo apt-get purge php-xdebug`.

Restart your PHP-FPM or your service/server.