nginx errors readv() and recv() failed

Regarding this error:

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

there was 1 more case where I could still see this. Quick set up overview:

  • CentOS 5.5
  • PHP with PHP-FPM 5.3.8 (compiled from scratch with some 3rd party modules)
  • Nginx 1.0.5

After looking at the PHP-FPM error logs as well and enabling catch_workers_output = yes in the php-fpm pool config, I found the root cause in this case was actually the amfext module (PHP module for Flash). There's a known bug and fix for this module that can be corrected by altering the amf.c file.

After fixing this PHP extension issue, the error above was no longer an issue.


I was using php-fpm in the background and slow scripts were getting killed after a said timeout because it was configured that way. Thus, scripts taking longer than a specified time would get killed and nginx would report a recv or readv error as the connection is closed from the php-fpm engine/process.


Update:

Since nginx version 1.15.3 you can fix this by setting the keepalive_requests option of your upstream to the same number as your php-fpm's pm.max_requests:

upstream name {
    ...
    keepalive_requests number;
    ...
}


Original answer:

If you are using nginx to connect to php-fpm, one possible cause can also be having nginx' fastcgi_keep_conn parameter set to on (especially if you have a low pm.max_requests setting in php-fpm):

http|server|location {
    ...
    fastcgi_keep_conn on;
    ...
}

This may cause the described error every time a child process of php-fpm restarts (due to pm.max_requests being reached) while nginx is still connected to it. To test this, set pm.max_requests to a really low number (like 1) and see if you get even more of the above errors.

The fix is quite simple - just deactivate fastcgi_keep_conn:

fastcgi_keep_conn off;

Or remove the parameter completely (since the default value is off). This does mean your nginx will reconnect to php-fpm on every request, but the performance impact is negligible if you have both nginx and php-fpm on the same machine and connect via unix socket.

Tags:

Nginx

Fastcgi