Explain the working principle of Nginx in simple terms

Nginx is known as a high-performance web server, which is inseparable from its architecture and working principle

 

After nginx starts, there will be a master process and multiple worker processes. The master process is mainly used to manage the worker process , including: receiving signals from the outside world, sending signals to each worker process, monitoring the running status of the worker process, and automatically restarting a new worker process when the worker process exits (in abnormal cases). . Basic network events are handled in the worker process. Multiple worker processes are peer-to-peer, they compete equally for requests from clients, and each process is independent of each other. A request can only be processed in one worker process, and a worker process cannot process requests from other processes. The number of worker processes can be set. Generally, we will set it to be consistent with the number of machine cpu cores.

 

By default, nginx provides services by a single process (master process). The advantages of configuring multiple processes at the same time in the operating environment according to the master-worker mode are mainly:

 

1. Since the master process does not provide services for user requests, it is only used to manage the worker processes that actually provide services, so the master process can be unique. It only focuses on its own pure management work and provides command line services for administrators. Including such as starting services, stopping services, reloading configuration files, smooth upgrade procedures, etc. The master process needs to have greater privileges. For example, the root user is usually used to start the master process. The permissions of the worker process must be less than or equal to the master process, so that the master process can completely manage the worker process. When an error occurs in any worker process that causes coredump, the master process will immediately start a new worker process to continue the service.

 

2. The processing of Internet requests by multiple worker processes can not only improve the robustness of the service (after a worker process fails, other worker processes can still provide services normally), and most importantly, it can make full use of the common SMP multi-core architecture, thus Realize true multi-core concurrent processing at the micro level. Therefore, it is definitely not appropriate to use one process (master process) to handle Internet requests.

 

There are roughly two reasons why the number of worker processes is set to be consistent with the number of machine cpu cores :

 

First, nginx generally only acts as a high-concurrency proxy, and basically does not use IO operations. It is a CPU-intensive operation, so it is basically completed instantaneously during processing, and IO blocking rarely occurs; second, the relationship between processes and CPU scheduling, a single core handles more When there are only one process, it is queued for processing, so it does not make much sense to set the number of worker processes to exceed the number of cpu cores.

 

How does the worker process handle http requests ?

 

First, each worker process is forked from the master process. In the master process, the socket (listenfd) that needs to be listened is established first, and then multiple worker processes are forked. The listenfd of all worker processes will become readable when a new connection arrives. To ensure that only one process handles the connection, all worker processes grab accept_mutex (shared lock) before registering the listenfd read event, and the process that grabs the mutex is registered listenfd read event, call accept in the read event to accept the connection. When a worker process accepts the connection, it starts to read the request, parses the request, processes the request, generates data, returns it to the client, and finally disconnects the connection. This is a complete request, and a request is completely It is handled by the worker process, and only in one worker process. The benefits of this approach are:

 

1. Save the overhead caused by locks. For each worker process, the independent process does not need to be locked, so the overhead caused by the lock is saved, and at the same time, it will be much more convenient when programming and problem checking.

 

2、独立进程,减少风险 。采用独立的进程,可以让互相之间不会影响,一个进程退出后,其它进程还在工作,服务不会中断, master进程则很快重新启动新的worker进程。当然,worker进程的异常退出,肯定是程序有bug了,异常退出,会导致当前worker上的所有请求失败,不过不会影响到所有请求,所以降低了风险。

 

nginx的事件处理机制,采用异步非阻塞事件处理机制,一个worker进程只有一个主线程,通过异步非阻塞的事件处理机制,实现了循环处理多个准备好的事件,从而实现轻量级和高并发。

 

异步非阻塞事件处理机制:

 

同步和异步的概念,这两个概念与消息的通知机制有关。同步的情况下,是由处理消息者自己去等待消息是否被触发,而异步的情况下是由触发机制来通知处理消息者。

 

阻塞和非阻塞,这两个概念与程序等待消息(无所谓同步或者异步)时的状态有关。

 

当读写事件没有准备好时,就放入epoll里面。如果有事件准备好了,那么就去处理;如果事件返回的是EAGAIN,那么继续将其放入epoll里 面。从而,只要有事件准备好了,我们就去处理,只有当所有时间都没有准备好时,才在epoll里面等着。这样,我们就可以并发处理大量的并发了,当然,这里的并发请求,是指未处理完的请求,线程只有一个,所以同时能处理的请求当然只有一个了,只是在请求间进行不断地切换而已,切换也是因为异步事件未准备好,而主动让出的。这里的切换是没有任何代价,你可以理解为循环处理多个准备好的事件。

 

与多线程相比,这种事件处理方式是有很大的优势的,不需要创建线程,每个请求占用的内存也很少,没有上下文切换,事件处理非常的轻量级。并发数再多也不会导致无谓的资源浪费(上下文切换)。更多的并发数,只是会占用更多的内存而已。

 

 参考原文  http://hzcsky.blog.51cto.com/1560073/1533354

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326678825&siteId=291194637