The overall architecture of Nginx

Nginx_Principles_and_Architecture_0.png

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:

  1. Receive signals from the outside world.
  2. Send a signal to each worker process.
  3. Monitor the running status of the woker process.
  4. 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:

  1. Multiple worker processes are peer-to-peer and independent of each other, and they compete equally for requests from clients.
  2. A request can only be processed in one worker process, one worker process, and it is impossible to process requests from other processes.
  3. 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.

Multi-process mechanism

There are two advantages to using multiple processes:

  1. 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.
  2. 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.

Memory pool

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.

 

Modular design

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.

Nginx_Principles_and_Architecture_1.png

 

data


  1. Talking about the Internal Core Architecture Design of Nginx Server

  2. Nginx architecture model analysis

  3. Dive into the architecture of Nginx

The article was last published on: 2019-11-09 17:13:28

Orignal link:https://blog.csdn.net/hustspy1990/article/details/102989305