DWQA QuestionsCategory: ServerDiscuss whether nginx and PHP FPM run in multi process and multi thread mode
In low litigation asked 2 months ago


In the configuration file of nginx, you can set how many nginx processes can be started, as follows:
worker_processes 2;
You can also set the maximum number of connections for each process as follows:
worker_connections 1024;


In the PHP FPM configuration file, you can set how many PHP FPM processes can be started, as follows:
pm.max_children = 5
You can also set the maximum number of connections for each process as follows:
pm.max_requests = 500


From the view of nginx configuration and PHP FPM configuration, they can start multiple processes, and each process can handle multiple connections,
According to my understanding:Nginx and PHP FPM both run in a multiprocess and multithreaded way;
I don’t know if I understand that right?

1 Answers
Best Answer
Zi Ting Wang answered 2 months ago

This problem is relatively elementary, and the information on the official documents is very complete.
Nginx isNonblocking IO & IO multiplexingModel, through the epoll like functions provided by the operating system, can handle the requests of multiple clients in one thread.
Nginx processes are threads, that is, there is only one thread in each process, but this thread can serve multiple clients.
Php-fpm is a blocking single thread model,pm.max_childrenSpecifies the maximum number of processes,pm.max_requestsSpecifies how many requests are processed by each process and then restarted (PHP needs to restart because of occasional memory leaks)
Php-fpm has only one thread per process, but a process can only serve one client at the same time.
Most Linux programs tend to use process rather than thread, because the cost of creating process is relatively small under Linux, and the thread function of Linux is not very powerful.

diaocow replied 2 months ago

Is nginx an asynchronous IO model? Why do I think it’s a non blocking IO IO multiplexing model (basically, the event library is the same), and then choose the optimal IO multiplexing mechanism on a specific machine (for example, choose epoll on Linux machine, use kqueue for FreeBSD, and select by default), and if nginx is configured to run in a multi process mode (fork out n workers’ U processes), it seems as long as the concept of “process” is emphasized Yes, it’s easy to confuse “threads”; finally, add a sentence to the owner: the number of worker connections settings can be larger, but it should be smaller than the maximum number of descriptors allowed to be opened by a process. In the configuration file, you can use worker rlimit nofile to break through the default 1024