2. Nginx working mode and some understanding of how to handle high concurrency
The working mode of Nginx is very simple, that is, it uses a master process and multiple worker processes. The role of the master process is also very clear: it is responsible. Manage the worker process and monitor connection requests at the same time. When the connection request arrives, the connection is put into the worker process to handle specific business requests, such as http requests. The reason why Nginx can handle high concurrency is that the socket management method is asynchronous and non-blocking. Select/poll/epoll/kqueue is used to manage a large number of socket descriptors. Each worker process has one main thread and no others. The advantage of threads is that there is no need to switch between threads, which saves resources. So in summary: The key to Nginx being able to support high concurrency while still running at low efficiency is that there are only a limited number of working processes and one listening process inside the entire system, and there is only one main thread inside each process, so there is no It will cause a lot of thread switching, thereby reducing system overhead. At the same time, each thread uses an asynchronous non-blocking method to manage descriptors, so that a large number of descriptors can be managed. When there are many descriptors, it will only occupy more memory. It only uses memory and does not take up a lot of CPU time. The above is the process model and event model of Nginx. There are three main situations handled in the event model, namely network events, such as HTTP requests, etc. Network events can be well solved using asynchronous non-blocking mode; and signals, Timers, signals and timers are not very clear yet. The way Nginx handles competition for system resources between processes: that is, the thundering herd phenomenon that exists between processes.
The above introduces the working mode and some understanding of Nginx, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.