Me: New to nginx and FastCGI in general, apologies if this is old hat and obvious, but Googling shows me a lot of people recommending random configuration values, but no one that explains the reasoning behind their decisions.
What's the relationship between an nginx worker_process and a PHP-FPM worker? Do more nginx workers means more PHP-FPM workers, or are they unrelated?
My understanding of how this works on a high level is
- User requests a web page
- nginx handles the request, sees it's a PHP page
- nginx hands request off to PHP-FPM
- PHP-FPM either handles the request with an existing process, or spawns new ones
What's unclear to me if there's any relationship between PHP-FPM spawning a new child processes, and which nginx worker processes are talking to it. i.e. if you had two identical web server, one with worker_process = 4
and another with worker_process = 8
, and they both saw identical web traffic, would one server create more php-fpm child processes than another? Or have I said something so stupid here that it shows I'm missing a fundamental concept? (if so, what?)
Not trying to solve a specific problem -- just trying to learn how to reason about the behavior of nginx and php-fpm so I can contribute to scaling and performance brainstorms/troubleshooting.
There is no inherent relation between nginx
worker_process
es and php-fpm children. They don't even have to be running on the same machine! But if they are on the same machine, they can contend for CPU (which usually isn't a problem as nginx requires very little CPU compared to PHP).Nginx worker processes connect to php-fpm. However, they do so by creating async connections, considers them as normal upstreams, just like any other.
Nginx is an event driven applications, meaning that most of the I/O stuff it does, especially connecting to upstreams, is async. So one worker process would, at any time, have any number of connections to an upstream.
PHP-FPM will spawn (or, ideally for performance reasons, use an already spawned but not used process) for each request it receives.
So, considering these, generally there is no direct relationship between the two. You will always have exactly as many worker processes in nginx as you define but the number of php-fpm processes will, most of the times, vary depending on the number of requests it receives, which is not directly related to the number of nginx worker processes.
One could argue that if you enable fastcgi keepalive in nginx, php-fpm processes would remain there longer. Considering that each worker process gets its own connection pool for upstreams that use keepalive, it could cause php-fpm to keep more processes running at all times for each worker process. However, fastcgi keepalive is broken in php-fpm as far as I am aware so it shouldn't be a problem :).