PHP-FPM occasional "FastCGI: failed to connect to server" error

6,502

Solution 1

Don't use a socket for this purpose. They block too much to support even a medium-load site with lots of requests coming in. Use a TCP socket instead to connect httpd to php-fpm.

Refer to this for instructions: https://wiki.apache.org/httpd/PHP-FPM (look at the section entitled "TCP socket (IP and port) approach").

You also don't have very many workers set up. I usually recommend around two per available CPU core (assuming that most web requests will take 500ms). You can do your own math based on your average response time. If you need more workers to support your load, get more servers.

Solution 2

I've been able to solve the connection errors. I did not have enough resources allocated to php-fpm. I've increased the amount of workers ( thanks Joel ), set a timeout limit ( thanks Froggiz ) and set a max_requests value. The following post explains in detail the issue I was having.

https://stackoverflow.com/questions/18009479/random-php-fastcgi-connection-reset-by-peer-incomplete-headers

Thanks!

Share:
6,502

Related videos on Youtube

Eko3alpha
Author by

Eko3alpha

Updated on September 18, 2022

Comments

  • Eko3alpha
    Eko3alpha over 1 year
    Ubuntu 14.04.3 LTS
    Apache 2.4.7
    PHP 5.5.9
    

    I switched from mod_php to PHP-FPM about two weeks ago. Everything, for the most part is running smoothly. Except twice now I've had a situation where apache/php would become unresponsive. A restart would fix the issue, however I'd like to know why this is happening. Here are the error logs, its filled with hundreds of the similar type of error.

    # /var/log/apache2/error.log
    .
    .
    [Wed Dec 16 23:19:21.476641 2015] [fastcgi:error] [pid 32523] (104)Connection reset by peer: [client xx.xx.xx.xx:43676] FastCGI: comm with server "/usr/lib/cgi-bin/php5-fcgi" aborted: read failed
    [Wed Dec 16 23:19:21.476866 2015] [fastcgi:error] [pid 32411] (2)No such file or directory: [client xx.xx.xx.xx:63082] FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed
    [Wed Dec 16 23:19:21.477489 2015] [fastcgi:error] [pid 32527] (104)Connection reset by peer: [client xx.xx.xx.xx:49675] FastCGI: comm with server "/usr/lib/cgi-bin/php5-fcgi" aborted: read failed
    [Wed Dec 16 23:19:21.478270 2015] [fastcgi:error] [pid 32548] (2)No such file or directory: [client xx.xx.xx.xx:59140] FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed
    .
    .
    

    Apache config

    # /etc/apache2/conf-available/php5-fpm.conf
    <IfModule mod_fastcgi.c>
        AddHandler php5-fcgi .php
        Action php5-fcgi /php5-fcgi
        Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
        FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
    
        <Directory /usr/lib/cgi-bin>
            Require all granted
        </Directory> 
    </IfModule> 
    

    PHP5-FPM Config ( included what I thought may be helpful )

    # /etc/php5/fpm/pool.d/www.conf
    listen = /var/run/php5-fpm.sock
    .
    .
    user = www-data
    group = www-data
    .
    .
    pm.max_children = 10
    pm.start_servers = 5
    pm.min_spare_servers = 5
    pm.max_spare_servers = 1
    pm.max_requests = 500
    .
    .
    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660
    .
    .
    

    Connecting to sockets

    # lsof -U | grep php
    php5-fpm  14373   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
    php5-fpm  17084   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
    php5-fpm  18544   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
    php5-fpm  18544   www-data    4u  unix 0xffff8800da1e7700      0t0 701371 /var/run/php5-fpm.sock
    php5-fpm  18649   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
    php5-fpm  19672   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
    

    Please let me know if there is any other information needed to help figure this out. As I've stated this has only happened twice now. Thanks

    • Froggiz
      Froggiz over 8 years
      Try to use -idle-timeout paramater on "FastCgiExternalServer" line to solve this problem.
    • Eko3alpha
      Eko3alpha over 8 years
      I'll give it a shot and see how it behaves the next few days.