Nginx Source Code Analysis-http Framework execution process

Source: Internet
Author: User

Nginx Source Code Analysis-http Framework execution process

The approximate flow in the HTTP framework's dynamic execution: First establish a TCP connection with the client, receive HTTP request lines, head and resolve their meaning, and then find some HTTP modules based on the nginx.conf configuration file, so that a partner can handle the request.

In order to control timeouts precisely, it is also necessary to place read and write events into the timer.

With the Ngx_handle_read_event method and the Ngx_handle_write_event method of the event module, we can add the corresponding event to the Epoll, and we can play the ngxin process when the event trigger condition is met Ngx_ The handler callback method of the Event_t event executes the business. The Ngx_add_timer method through the event module can add the above read event or write event to the timer, and after the timeout condition is met, the Nginx process also invokes the handler callback method that invokes the Ngx_event_t event to execute the business.

When a TCP connection is established successfully, the first time a readable event occurs on a TCP connection, the Ngx_http_init_request method is called to initialize the HTTP request, and the HTTP framework does not begin the initialization of the request after the connection is established successfully. Instead, the socket buffer of the connection does not take place until the user sends the requested content.

Each HTTP module can set the context structure for a request and set and get the context through NGX_HTTP_SET_CTX and NGX_HTTP_GET_MODULE_CTX macros, which are pointers to the context structure of the request settings. is actually saved to the ngx_http_request_t struct in the CTX pointer array.

Ngx_http_core_content_phase is the checker method of the ngx_http_content_phase phase, which can be said to be one of the most common stages in the development of HTTP modules, Ngx_http_content_ The Pahse phase is used to actually process the requested content. The HTTP modules in the remaining 10 phases are handled in a global ngx_http_core_main_conf_t structure, meaning that they are valid for any HTTP request. However, there is a natural need in the ngx_http_content_phase phase, and some HTTP modules may only be expected to take effect for a particular request at this stage of processing the request content, not for all requests. For example, if the requested URI matches one of the locatin blocks in the configuration file, select an HTTP module according to the configuration under the location block to perform his handler processing and replace the Ngx_http_content_ Other handler methods in the phase phase (these handler methods are not executed for the request), this method is set to handler in ngx_http_core_loc_conf_t

Processes the POST request, calling the Ngx_http_run_poted_requests method to process the POST request.

If a request needs to deal with more than one upstream server at the same time to handle multiple TCP connections, then it needs to handle too many events, which makes the module difficult to maintain, by using the subrequest mechanism

A request is typically made up of a required HTTP request line, a request header, and an optional package body.

The Ngx_http_send_header method is responsible for the corresponding line and header of the paparazzi HTTP, and sends them to the client. He is a filter list, the last node is responsible for sending, is the filter module Ngx_http_header_filter_modlue provided by the Ngx_http_header_filter method will be based on HTTP rules Headers_ The member variable in Out is serialized as a character stream, what if you can't send the corresponding hair out at once? This requires the member of the ngx_chain_t type in the ngx_http_request_t struct to be out, which will save the corresponding header without sending it, and how to send the remaining corresponding headers, which need to be combined with the end request Ngx_http_finalize_ The request method.

When the Ngx_http_header_filter method cannot send HTTP headers at one time, the following two occurrences occur, and the remaining corresponding headers are saved in the requested out member, and the Ngx_http_header_filter method returns Ngx_again .

If this response does not have a package, then it is usually possible to call the Ngx_http_finalize_request method to end the request, the second parameter of this function is critical, we need to pass ngx_again in, so Ngx_http_finalize_ The request method understands that the HTTP framework is actually required to continue sending a stream of remaining response characters saved in the request out member. The Ngx_http_finalize_request method sets the Write_event_handler member of the request to the Ngx_http_write method, so that when there is a writable event on the connection, the NGX_HTTP_ is called The Write method continues to send the remaining HTTP responses. The Ngx_http+finalize_request method combined with the second parameter can have a lot of in-process execution.

Nginx Source Code Analysis-http Framework execution process

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.