Whenever we receive a message like follows:
(104: Connection reset by peer) while reading response header from upstream,
Generally, we can blame the upstream side of this kind of error.
Here the connection was reset by the upstream peer and not by nginx itself. So Nginx as a client cannot do anything to make it right.
I am thinking if modifying buffer size will do the magic. Basically the command will change the buffer size where response headers are cached. This will take effect only when the response header is too big and in that case we receive a following message saying:
upstream sent too big header while reading response header from upstream, and please note that it is totally different thing from connection reset by peer.
As this kind of error is triggered randomly, I would suggest you check whether your nginx uses keepalive when talking to upstreams. If this was the case then the connection may be reset by upstream server when the idle timed out whereas nginx had no idea that the connection had been dropped, so forwarding the request using the same connection.
You can set a keepalive_timeout value to the upstream connection pool in nginx to avoid the problem.