502 Gateway Errors under High Load (nginx/php-fpm)

Solution 1:

This should fix it...

You have: fastcgi_buffers 4 256k;

Change it to: fastcgi_buffers 256 16k; // 4096k total

Also set fastcgi_max_temp_file_size 0, that will disable buffering to disk if replies start to exceeed your fastcgi buffers.

Solution 2:

Unix socket accept 128 connections by default. It is good to put this line into /etc/sysctl.conf

net.core.somaxconn = 4096

Solution 3:

If it's not helping in some cases - use normal port bind instead of socket, because socket on 300+ can block new requests forcing nginx to show 502.