nginx recv() failed (104: Connection reset by peer) while reading response header from upstream

Solution 1:

Such errors usually occurs when server is running out of resources, assuming that you're running most recent stable versions of php5-fpm:

  1. Check that php5-fpm has enough memory (there's no oom-killer killing the process)

  2. There's enough space on the disk

  3. Make sure to check the open file limits on the server. You're interested especially in the hard limit (-Hn):

    $ ulimit -Hn
    4096
    $ ulimit -Sn
    1024
    

Check number of currently opened file descriptors on the server:

    sysctl fs.file-nr
    fs.file-nr = 1440       0       790328

Modern servers are capable of handling many files, usually ulimits are set to unnecessary low values.

Then check nginx.conf, at the beginning there's something like:

    worker_processes 4;
    events {
      worker_connections 1024;
    }

If you're proxying request for each connection you'd need 2 file handles. Which means that in case of many connections you'd reach the limit quite quickly.

nginx has a worker_rlimit_nofile directive for restricting opened files per worker process (top level directive like worker_processes 4;):

    worker_rlimit_nofile    1024;

Just do the math and calculate how many opened file descriptors you'd need when all connections are used (a bit extreme case). Also consider all other services running on that server.