In the context of formally providing the product, deploying Nginx uses the master process to manage multiple worker processes.
In general, worker processes are equal to the number of cores on the server, and each worker process is busy, and they truly provide Internet services.
The master process is very quiet and is only responsible for monitoring the worker process.
Load balancing between worker processes through shared memory, atomic operations, and a series of communication between processes
Shared memory: Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest IPC approach and is specifically designed for low-efficiency operation of other interprocess communication modes. It is often used with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
The relationship between nginx processes after deployment, as shown in:
So why do you configure multiple processes in Master-worker mode: two points
1.master processes can be unique, focusing only on managing worker processes that actually provide services
2. Multiple worker processes improve the robustness of the service and leverage existing SMP architectures (heap into multi-processing architectures) for true multicore concurrency processing
So why the worker process is designed to match the number of CPU cores, let's see what the difference between Apache and Nginx is:
Apache a process processing a request, in the case of large concurrency, can only increase the number of processes or threads, usually a server hundreds of processes, a large number of process switching resulting in unnecessary system resource consumption
Nginx A worker process can handle the number of requests only limited to the size of the memory, different worker process processing concurrent requests do not have a synchronization lock limit, the worker process does not sleep, so set the worker process is consistent with the CPU number of cores, the cost of switching between processes is minimal. (that is, the CPU is a 8-core, and the worker process is set to 8.) (Supplemental Knowledge: the relationship between process and CPU scheduling, when a single core processes multiple processes, is queued, so the number of worker processes is set beyond the core number is not significant, resulting in inter-process switching overhead)
The relationship between Nginx processes in operation