I have the same problem and I think I, probably, know what's causing it.
The bad thing is that I got it trying to solve another one and I don't
know how to fix both :)
So, until today, my server sometimes issued 504 error. The error log
reported "upstream timed out (110: Connection timed out) while reading
response header from upstream" error. Trying to fix it, I played with
various PHP-FPM timeouts. So, I increased pm.max_children from 5 to 10
and set request_terminate_timeout to 30s. Immediately after these
changes had been made, 504 error was gone, but now I receive 502 with
the same diagnostics as yours: "Connection reset by peer".
Do you have request_terminate_timeout defined in your php-fpm.conf?
you have too many connections and FPM is not able to handle
everything. You can play with backlog, max_client and timeouts.