Nginx – PHP-FPM with nginx load balancing long request blocks all other requests

load balancingnginxphp-fpm

I have three servers, the load balancer runs nginx and passes the PHP requests upstream to one of two servers running php-fpm.

I was actually trying to test concurrency in the first place, so the php script on each PHP-FPM server shows a start and end time as well as the hostname, and after the start time is echoed it uses 100% CPU for 5 seconds before echoing the end time.

Neither server hits 100% CPU simultaneously for 4 concurrent requests and the timestamps show they are served consecutively which makes me think nginx and fastcgi is blocking all concurrent connections.

Running ab with 100 concurrent connection sees all processes on one PHP-FPM server (out of 10 available) processing and the other server is completely quiet doing nothing.

The nginx conf is:

upstream  backend  {
     server   192.168.1.60:9000;
    server   192.168.1.61:9000;  
}

server {
    listen   80;
    server_name  localhost;
    access_log  /var/log/nginx/localhost.access.log;

    location / {
        root   /var/www;
        index  index.php;
    }


    location ~ .php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
        include fastcgi_params;
    }

}

Best Answer

Don't go passing FCGI over the network; use regular ol' HTTP for as long as you can. You're entering poorly-tested waters the way you're going.

At any rate, I really don't recommend using nginx as a load balancer. It really isn't the best (or even a "good enough") tool for the job. I think the best option is Linux Virtual Server, as it is transparent to the TCP connections and blisteringly fast, but if that's off the table for some reason, at the very least use haproxy.