In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how Nginx handles network events". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In order to gain an in-depth understanding of the various principles of Nginx and the handling of some error scenarios in extreme scenarios, we need to first understand what network events are.
Nginx is an event-driven framework, the so-called event mainly refers to network events, Nginx each network connection will respond to two network events, one read event and one write event. In order to gain an in-depth understanding of the various principles of Nginx and the handling of some error scenarios in extreme scenarios, we need to first understand what network events are.
Network transmission
Next, take a look at the picture above. For example, host An is a laptop at home, and host B is a server with Nginx services running on it. What are the main events experienced in the process of sending a GET request for HTTP from host A to host B. As can be seen from the data flow section of the above figure:
A GET request is sent in the application layer-> to the transport layer. The main thing to do in this step is that the browser opens a port, which can be seen in the task manager of windows. He will write down this port and record the ports opened by Nginx, such as 80 or 443, to the transport layer-> and then in the network layer will record the IP where our host is located and the target host. That is, after the Nginx server public network IP-> to the link layer-> via Ethernet-> arrives at the home router (network layer), the home router will record some of the next segment of the IP of the operator-> through the wide area network-> jump to the machine where host B is located-> the message will pass through the link layer-> network layer-> to the transport layer. At the transport layer, the operating system knows that it is for the process that has opened 80 or 443, which is naturally Nginx-> then Nginx will process the request in his HTTP state processor (application layer).
What kind of role does the network message play in the above process?
TCP flow and message
The data link layer adds the source MAC address and the source destination address to the front Header and Footer parts of the data-> to the network layer, the public network address of the Nginx (destination IP address) and the public network address of the browser (source IP address)-> to the TCP layer (transport layer), specifies the port opened by the Nginx (destination port) and the port opened by the browser (source port)-> then the application layer is the HTTP protocol.
This is a message, that is to say, the HTTP protocol we send will be cut into many small messages, which are called MTU at the network layer, and each MTU of Ethernet is 1500 bytes. In the TCP layer (transport layer), uh, the largest MTU value in each link is considered. At this time, each message usually has only a few hundred bytes. The size of this message is called MSS, so every time a message with a MSS less than this size is received is actually a network event.
At this point, let's take a look at how many events in the TCP protocol relate to some of the interfaces we call every day (such as Accept, Read, Write, Close).
TCP Protocol and non-blocking Interface
The request to establish a TCP connection event actually sends a TCP message that reaches the Nginx through a process like the one explained in the second part above, corresponding to the read event. Because for Nginx, I read a message, so it is the Accept link event.
If it is a TCP connection readable event, a message is sent, and it is also a read event for Nginx, which is a Read read message.
If the peer (that is, the browser) shuts down actively, the windows operating system will send an event asking for the link to be closed, which is still a read event for Nginx, because he is just going to read a message.
So what is writing events? When our browser needs to send a response to the browser, we need to write the message to the operating system and ask the operating system to send it to the network. This is a write event.
For some network read and write events like this, usually in Nginx or any framework for handling asynchronous events, he will have something called event collection and dispatcher. The consumer that defines each type of event handling, that is, the event is a producer, is automatically produced into our Nginx through the network, and we want to create a consumer for each event. For example, the connection establishment event consumer calls Accept, and the HTTP module establishes a new connection. There are also a lot of reading or writing messages, and different methods are called at different times in the HTTP state machine, that is, each consumer handles it.
The above is an event distribution, consumer, including AIO events like asynchronous read and write disk events, as well as timer events, such as worker_shutdown_timeout.
Nginx network event instance
The above describes the sending of network messages and the corresponding network events in Nginx. For example, when Accept establishes a new connection, it actually receives a read event. Then we analyze how to make Nginx receive the read event when establishing a three-way handshake by grabbing packets. The packet grabbing tool is Wireshark.
First, we install the Wireshark software, grab the IP and port where the Nginx is located, and then visit the page to say two main things in the TCP layer:
The browser will first open this page, where a port 1875 is opened locally, while port 8080 is launched by Nginx.
The TCP layer mainly does the communication between processes.
The IP layer mainly solves the problem of how to find each other between machines.
The three-way handshake means that windows sends [SYN] to Nginx first, then the server where Nginx is located will also send a [SYN] to windows. At this time, Nginx is not aware of it, because the connection is still half-open. It is not until the windows server sends [ACK] to the server where Nginx is located that the operating system of Nginx notifies Nginx that we have received a read event, which corresponds to the establishment of a new connection, so Nginx should call the Accept method to establish a new connection.
That's all for the content of "how Nginx handles network events". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.