Request in Nginx

Source: Internet
Author: User
In this section we talk about request, in Nginx we refer to HTTP requests, specific to NGINX data structure is ngx_http_request_t. Ngx_http_request_t is the encapsulation of an HTTP request. We know that an HTTP request consists of a request line, a request header, a request body, a response line, a response header, and a response body.

The HTTP request is a typical request-response type of the network protocol, and HTTP is the file protocol, so we parse the request line with the request header, and the output response line with the response header, often a line of processing. If we write an HTTP server ourselves, usually after a connection is established, the client sends a request. We then read a row of data, analyzing the method, URI, and http_version information contained in the request line. Then one line of processing the request header, and based on the request method and the request header information to determine whether the request body and the length of the request body, and then to read the request body. Upon request, we process the request to produce the data that needs to be output, and then generate the response line, the response header, and the response body. After the response is sent to the client, a complete request is processed. Of course, this is the simplest way to handle the webserver, in fact, Nginx is also doing this, but there are some small differences, for example, when the request header read completed, the request processing began. Nginx uses ngx_http_request_t to save data that resolves requests related to output responses.

Then, let's briefly talk about how Nginx handles a complete request. For Nginx, a request starts from Ngx_http_init_request, in which the Read event is set to Ngx_http_process_request_line, that is, the next network event, will be NGX_HTTP_ Process_request_line to execute. From the Ngx_http_process_request_line function name, we can see that this is to handle the request line, just as before, the first thing to do is to handle the request line is consistent. The request data is read through Ngx_http_read_request_header. Then call the Ngx_http_parse_request_line function to parse the request line. Nginx to improve efficiency, the use of state machine to parse the request line, and in the comparison of method, do not directly use the string comparison, but instead of four characters into an integer, and then a comparison to reduce the number of CPU instructions, this is said earlier. Many people may be aware that a request line contains the requested method, Uri, version, but does not know that in fact in the request line, it can also contain the host. such as a request get Http://www.taobao.com/uri http/1.0 Such a request line is also legal, And host is www.taobao.com, at this time, Nginx ignores the host domain in the request header, and looks for the virtual host in the request line. In addition, for the http0.9 version, is not support for the request header, so here is also to special handling. Therefore, when parsing the request header later, the protocol version is either 1.0 or 1.1. The parameters that the entire request line resolves to are saved to the ngx_http_request_t structure.

After parsing the request line, Nginx sets the handler of the Read event to Ngx_http_process_request_headers, and then the subsequent request is Ngx_http_process_request_ Read and parse in the headers. The Ngx_http_process_request_headers function is used to read the request header, as with the request line, or to call Ngx_http_read_request_header to read the request header, calling Ngx_http_parse_ Header_line to parse a row of request headers, the parsed request header is saved to the ngx_http_request_t domain headers_in, and headers_in is a list structure that holds all the request headers. Some requests in HTTP require special handling, and these request headers and request handlers are stored in a mapping table, that is, ngx_http_headers_in, when initialized, generates a hash table that, when parsed into a request header, is first found in the hash table. If one is found, the corresponding handler is called to handle the request header. For example: The handler function of the host header is ngx_http_process_host.

When nginx resolves to a two carriage return line break, it represents the end of the request header, and Ngx_http_process_request is called to process the request. Ngx_http_process_request sets the read-write event handler for the current connection to Ngx_http_request_handler, and then calls Ngx_http_handler to really start processing a full HTTP request. It may be strange here that the read-write event handler is Ngx_http_request_handler, and in this function, the Read_event_ in ngx_http_request_t is called according to whether the current event is a read event or a write event. Handler, or Write_event_handler. Because at this time, our request header has been read completed, previously said, Nginx practice is not to read the request body, so this inside we set Read_event_handler for ngx_http_block_reading, that is, do not read the data. Just said, really start processing data, is in Ngx_http_handler this function, this function will set Write_event_handler to Ngx_http_core_run_phases, and execute ngx_http_core_ The Run_phases function. Ngx_http_core_run_phases This function performs multi-stage request processing and nginx divides the processing of an HTTP request into multiple stages, then this function executes these stages to produce the data. Since ngx_http_core_run_phases will eventually produce data, it is easy to understand why the Write event handler function is ngx_http_core_run_phases. Here, I briefly explain the invocation logic of the function, we need to understand that the final call ngx_http_core_run_phases to handle the request, the resulting response header will be placed in the ngx_http_request_t headers_out, this part of the content, I'll put it in the request processing process. The various stages of NGINX will process the request, and finally call filter to filter the data, processing the data, such as truncked transmission, gzip compression, etc. The filter here includes the header filter and the body filter, which is the response head or response body to be processed. Filter is a list structure, with the header filter and the body filter, first execute the header filterAll filter, and then execute all the filter in the body filter. In the header filter, the last filter, the Ngx_http_header_filter, this filter will traverse all the response headers, finally the response header needs to be output in a contiguous memory, and then call Ngx_http_write_ Filter for output. Ngx_http_write_filter is the last of the body filter, so the first body information of Nginx, after passing through a series of body filter, will finally call Ngx_http_write_filter to output ( There is a diagram to illustrate).

Note here that Nginx will put the entire request header in a buffer, the size of the buffer is set by the configuration item client_header_buffer_size, if the user's request header is too large, this buffer can not be installed, The Nginx will reassign a new larger buffer to mount the request header, the large buffer can be set by Large_client_header_buffers, this large_buffer set of buffer, such as configuration 4 8k, It means that there are four 8k sizes of buffer to use. Note that in order to preserve the integrity of the request line or the request header, a complete request line or request header needs to be placed in a contiguous memory, so a complete request line or request header will only be stored in a buffer. This will return a 414 error if the request row is larger than the size of a buffer, and a 400 error will be returned if a request header size is larger than a buffer size. After understanding the values of these parameters, and the actual practice of Nginx, in the application scenario, we need to adjust these parameters according to the actual requirements to optimize our program.

Process Flowchart:

These are the lifetime of an HTTP request in Nginx. Let's look at some of the concepts associated with the request.

The above describes the Nginx request, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.