Nginx Server web request processing mechanism, nginxweb Mechanism

Source: Internet
Author: User

Nginx Server web request processing mechanism, nginxweb Mechanism

I. Detailed explanation of the web Request Processing Mechanism of the nginx Server

In terms of the design architecture, nginx servers are different. The difference lies in its modular design and the Processing Mechanism of client requests.

The relationship between the web server and the client is one-to-many. The web server must be able to provide services for multiple clients at the same time. Generally, there are three methods to complete concurrent request processing: multi-process, multi-thread, and one-step.

1. multi-process mode

Multi-process mode means that when the server receives a client, the master process of the server will survive a sub-process and establish a connection with the client for interaction until the connection is disconnected, the child process is finished.

The advantage of the multi-process approach is that the design and implementation are relatively simple. Each sub-process is independent of each other, and the process of processing client requests is not disturbed by each other. When a sub-process has problems, it is not easy to extend the impact to other processes, which ensures the stability of the provision of services. When a sub-process exits, the resources it occupies will be recycled by the operating system, which will incur additional overhead in terms of resources and time. Therefore, if the web Server accepts a large number of concurrent requests, this will cause pressure on system resources, resulting in a decline in system performance.

In the early stage, Apache server provided external services in this way. To cope with a large number of concurrent requests, the Apache server uses the pre-Generated Process mechanism to transform the multi-process method. The pre-generated process generates the sub-process in advance, and is generated before the client request arrives. When the request arrives, the main process allocates a sub-process to interact with the client, after the interaction is complete, the process does not end, and is managed by the main process to wait for the next client request to arrive. The improved multi-process method relieves the pressure on system resources on the web server when a large number of concurrent requests exist. However, because the Apache server adopts the multi-process method in the initial architecture design, this cannot fundamentally solve the problem.

2. Multithreading

The multithreading mode is similar to the multi-process mode. It means that when a server receives a client, the main process of the server derives a thread to interact with the client.

Because the overhead of a thread generated by the operating system is far less than that of a process, the multi-thread method reduces the requirements of the web server on system resources to a large extent. This method uses threads for task scheduling. Development can follow certain standards, which is relatively standard and conducive to collaboration. However, this method is insufficient in terms of thread management. Multiple Threads are located in the same process. They can access the same memory space and interact with each other. At the same time, developers must manage the memory themselves during the development process, it increases the risk of errors. The server system needs to run continuously for a long time, and the accumulation of operations may eventually have a major impact on the entire server.

The iis server provides external services in multi-thread mode, which is relatively stable. However, experienced web server managers usually regularly check and restart the servers, to prevent unexpected faults.

3. asynchronous mode

Synchronous and asynchronous mechanisms in network communication are the concepts of communication modes. The synchronous mechanism means that the sender sends the next request after receiving the response from the receiver. the asynchronous mechanism is the opposite of the synchronous mechanism. In the asynchronous mechanism, after the sender sends a request, the sender continues sending the next request without waiting to receive the response. In the synchronization mechanism, all requests are synchronized on the server, and the sender and receiver process the requests at the same pace. In the asynchronous mechanism, all requests from the sender form a queue, the receiver notifies the sender after processing is complete.

Blocking and non-blocking are used to describe the way the process processes process calls. In network communication, it mainly refers to the blocking and non-blocking methods of the network socket, and the essence of the socket is the io operation. The blocking call method of socket is that the current thread is suspended from the running status until the call result is returned. After obtaining the CPU, the current thread continues to run; the non-blocking call method of the socket is the opposite of the blocking method. In the non-blocking mode, if the call result cannot be returned immediately, the current thread will not be suspended, but will return immediately to execute the next call.

Synchronous blocking mode. After sending a request to the receiver, the sender waits for a response. If the io operation performed by the receiver when processing the request cannot obtain the result immediately, the sender will wait until the result is returned, you cannot perform other work during this period. For example, when a supermarket queues for payment, the customer (sender) needs to wait for the Cashier (receiver) to make a payment (send a request) and wait for the cashier to change to 0. During this period, nothing else can be done; the cashier must wait for the cash register to return the result (I/O operation) Before giving the change to the customer (Response Request). During this period, the cashier can only wait and cannot do other things. This method is simple but inefficient.

Non-blocking synchronous mode. After sending a request to the receiver, the sender waits for a response. If the io operation performed by the receiver when processing the request cannot obtain the result immediately, the receiver returns immediately and does other tasks, however, because the request processing result is not obtained and the sender is not responded, the sender waits. After the I/O operation is complete, the receiver receives the result and responds to the sender. Then, the receiver enters the next request process. This method is not used in practice.

Asynchronous blocking mode. After sending a request to the receiver, you do not have to wait for a response. You can perform other operations. If the receiver does not obtain the result immediately when processing the request, wait until the result is returned before responding to the sender. Other work cannot be performed during this period. This method is not used in practice.

Asynchronous non-blocking mode. After sending a request to the receiver, you can continue other work without waiting for a response. If the io operation performed by the receiver when processing the request cannot obtain the result immediately, instead, return immediately to do other things. After the I/O operation is complete, the receiver is notified of the completion status and result, and the receiver then responds to the sender. Continue to use the example of queuing payment at the supermarket. After the customer (sender) makes a payment (Send request) to the Cashier (receiver), the customer can do other things, such as making phone calls and chatting, while waiting for the cashier to change; the cashier can help the customer package the goods while waiting for the cash machine to process the transaction (I/O operation). When the cash machine produces the result, the cashier pays the customer (Response Request ). Among the four methods, this is the most efficient way for the sender and receiver to communicate.

4. How does the nginx server handle requests?

A significant advantage of nginx server is its ability to process a large number of concurrent requests at the same time. It provides external services based on the multi-process mechanism and asynchronous mechanism. The asynchronous mechanism uses the asynchronous non-blocking mode.

After the nginx server is started, one master process and multiple worker processes can be generated. You can specify the number of worker processes in the configuration file. All processes on the nginx server are used to receive and process client requests. This is similar to the improved multi-process mechanism used by Apache, which generates multiple working processes in advance and waits for processing client requests.

Each worker process uses an asynchronous non-blocking method to process multiple client requests. When a working process receives a request from the client, it calls io for processing. If the result cannot be obtained immediately, it processes other requests. During this period, the client does not need to wait for a response, other tasks can be processed. When I/O calls return results, the worker process is notified. The worker process is notified, and the transaction is temporarily suspended to respond to client requests.

When the number of client requests increases and the network load is heavy, the nginx server uses a multi-process mechanism to ensure that the pressure on system resources is not increased; at the same time, the asynchronous non-blocking method reduces the congestion delay of the work process on io calls, and ensures that the request processing capability is not reduced.

2. event-driven model of the nginx Server

The event-driven model is one of the important mechanisms for nginx servers to ensure complete functions and good performance.

The nginx server responds to and processes web requests based on the event-driven model. The event-driven processing library is also called the multi-channel io multiplexing method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.