Nginx Principles and Architecture
The overall architecture of Nginx
There is a master process and multiple worker processes in Nginx. The master process does not process network requests. It is mainly responsible for scheduling work processes: loading configuration, starting work processes, and non-stop upgrades. The worker process is responsible for handling network requests and responses.
The master process is mainly used to manage the worker process, including the following four main functions:
- Receive signals from the outside world.
- Send a signal to each worker process.
- Monitor the running status of the woker process.
- When the woker process exits (in case of an exception), the new woker process is automatically restarted.
The woker process is mainly used to handle basic network events:
- Multiple worker processes are peer-to-peer and independent of each other, and they compete equally for requests from clients.
- A request can only be processed in one worker process, one worker process, and it is impossible to process requests from other processes.
- The number of worker processes can be set, generally we will set the same as the number of machine cpu cores. At the same time, in order to make better use of multi-core features, nginx has the cpu binding option, we can bind a certain process to a certain core, so that the cache failure will not be caused by the process switching.
How does Ngnix achieve high performance?
Event driven model
The asynchronous and non-blocking event-driven model can be said to be a key factor for Nginx to achieve high concurrency and high performance. This is similar to Netty, which uses BSD kqueue, Linux epoll, and Solaris event ports.
There are two advantages to using multiple processes:
- Resources are not shared between processes, and no locks are required, which reduces the impact of using locks on performance, while reducing programming complexity and reducing development costs.
- Using a separate process allows the processes to not affect each other. If a process exits abnormally and other processes work normally, the master process starts a new worker process quickly, ensuring that the service is not interrupted, thereby reducing the risk to lowest.
In order to avoid memory fragmentation, reduce the number of memory requests to the operating system, and reduce the development complexity of each module, Nginx designed a simple memory pool, which is mainly used to integrate the operation of applying memory to the system multiple times. Significantly reduced CPU resource consumption while reducing memory fragmentation.
The highly modular design is the foundation of Nginx’s architecture. Openresty introduced third-party modules such as lua on Nginx to make the extension more convenient.
Talking about the Internal Core Architecture Design of Nginx Server
Nginx architecture model analysis
Dive into the architecture of Nginx
The article was last published on: 2019-11-09 17:13:28