Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Nginx handles events

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how Nginx handles events". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how Nginx handles events".

Nginx event loop

When Nginx is just started, while waiting for the event part, that is, port 80 or 443 is open, we are waiting for a new event to come in. For example, the new client connects to Nginx and initiates a connection to us. This step often corresponds to the epoll wait method of epoll. At this time, Nginx is actually in a process state of sleep. When the operating system receives a handshake message to establish a TCP connection and processes the handshake process, the operating system notifies epoll wait of the blocking method, tells it that it can go down, and wakes up the Nginx worker process.

After going down, the operating system will go to the operating system to ask for events, and the operating system will put its prepared events in the event queue, from which you can get the events that need to be processed. Such as establishing a connection or receiving a TCP request message.

After it is taken out, the event will be looped, as shown above: when it is found that the queue is not empty, the event will be taken out and started to process the event. In the process of handling the event, a new event may be generated. For example, if you find a new connection has been established, you may have to add a timeout, such as the default time of 60 seconds, that is, if the browser does not send a request to Nginx within 60 seconds, Nginx will close the connection. For example, when Nginx discovers that it has received a complete HTTP request, it can generate a HTTP response. Then the need for this response is that Nginx can write the response to the write cache center of the operating system, requiring the operating system to send such a response to the browser as soon as possible, that is to say, new events may occur during processing. It is the event queue part that the loop processing event part points to, waiting for the next time to process it.

If all the events have been processed, they will return to waiting for the event.

After learning the Nginx event cycle, we understand that sometimes we use some third-party modules, which may do a lot of CPU operations, which can lead to a very long time to process an event. In the above flow chart, we can see that it will cause a large number of events in the queue to go unprocessed for a long time, thus triggering a vicious circle, that is, their timeout may have expired. A large number of CPU, Nginx tasks are consumed in dealing with abnormal disconnection, so Nginx can not tolerate some third-party modules to consume a lot of CPU computing tasks for a long time is such a reason. We can see modules like GZIP, which do not use a lot of CPU at a time but are used in segments, which are related to the event loop of Nginx.

Summary

This article mainly explains how Nginx handles events and how the process of Nginx event loop is, laying the groundwork for the next step to explain how Nginx event loop processes get events waiting to be processed from the operating system, and through the event loop to understand why Nginx does not expect a large number of CPU computing tasks in third-party modules.

Thank you for your reading. the above is the content of "how Nginx deals with events". After the study of this article, I believe you have a deeper understanding of how Nginx handles events, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 205

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report