2
votes

I am trying to use Nginx + php-fpm with nginx option 'keepalive' & 'fastcgi_keep_conn on' to keep tcp connection active between them but facing errors after serving few hundred requests "104: Connection reset by peer".

These errors are visible with php-fpm started on tcp port ( 9000 ) or unix socket ( /var/run/php5-fpm.socket ).

Intension here is to reduce new tcp/socket connection overhead between Nginx + php-fpm as much as possible and reuse connections as much as possible. Note that i have kept nginx 'keepalive 20' where as php-fpm 'pm.max_requests = 0' & 'pm.start_servers = 50'.

Can anybody please help me to fix this error?

Softwares in use:

    nginx version: nginx/1.4.7
    php-fpm version: 5.4.25 / 5.6.6

PHP-FPM Error log entry:

    WARNING: [pool www] child 15388 exited on signal 15 (SIGTERM) after 2245.557110 seconds from start 
    NOTICE: [pool www] child 18701 started

Nginx Errors:

with php-fpm listening on port 9000

    [error] 32310#0: *765 readv() failed (104: Connection reset by peer) while reading upstream, client: 10.10.133.xx, server: 192.168.28.xxx, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "10.10.133.xxx"

with php-fpm listening on socket /var/run/php5-fpm.socket

    [error] 14894#0: *383 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.10.133.xx, server: 192.168.28.xxx, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.socket:", host: "10.10.133.xxx"

Following is the nginx vhost conf

    upstream fastcgi_backend {
        server 127.0.0.1:9000;
        #server unix:/var/run/php5-fpm.socket;
        keepalive 30;
    }
    server {
            listen  80;
            server_name 10.10.xxx.xxx;
            access_log /tmp/ngx_access_80.log;
            error_log /tmp/ngx_error_80.log;

            location ~ \.php$ {
                root           /var/www/test/;
                include        fastcgi_params;
                fastcgi_pass   fastcgi_backend; //upstream set above
                fastcgi_keep_conn on; #Test for keepalive connection to php-fpm
                fastcgi_buffer_size 16k;
                fastcgi_buffers 4 16k;
            }
    }

Following is the php-fpm.conf

    [global]
    pid = /var/run/php-fpm-9000.pid
    error_log = /var/log/php-fpm-9000.log

    [www]
    listen = 0.0.0.0:9000
    user = daemon
    group = daemon
    rlimit_files = 60000

    pm = dynamic
    pm.max_requests = 0

    pm.max_children = 500
    pm.start_servers = 50
    pm.min_spare_servers = 40
    pm.max_spare_servers = 90 
3

3 Answers

1
votes

You must set nginx keepalive_requests and php-fpm pm.max_requests to the same value to avoid getting this error

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

If the two values are not matching, then either nginx or php-fpm end up closing the connection, triggering the error.

-2
votes

There is a bug with php-fpm which makes it fall over when used with nginx's

fastcgi_keep_conn= on;

You need to turn that option to off.

-2
votes

this indicates somehow the php-cgi child 15388 has received a SIGTERM from the OS or PHP-FPM . See https://bugs.php.net/bug.php?id=60961