Request in nginx

Source: Internet
Author: User
: This article mainly introduces the request in nginx. if you are interested in the PHP Tutorial, you can refer to it. In this section, we will talk about request. in nginx, we refer to http requests. Specifically, the data structure in nginx is ngx_http_request_t. Ngx_http_request_t is the encapsulation of an http request. We know that an http request contains the request line, request header, request body, response line, response header, and response body.

Http requests are typical request-response network protocols, while http is a file protocol. Therefore, we are analyzing request lines and request headers, as well as outputting response lines and response headers, it is usually processed by a row. If we write an http server by ourselves, the client will send a request after a connection is established. Then we read a row of data and analyze the method, uri, and http_version information contained in the request line. Then, the request header is processed in one row, and the request body length and request body length are determined based on the request method and request header information. then, the request body is read. After obtaining the request, we process the request to generate the data to be output, and then generate the response line, response header, and response body. After the response is sent to the client, a complete request is processed. Of course, this is the simplest webserver processing method. In fact, nginx does the same thing, but there are some small differences. for example, after the request header is read, it starts to process the request. Nginx uses ngx_http_request_t to save the data related to the resolution request and the output response.

Next, let's briefly explain how nginx handles a complete request. For nginx, a request starts from ngx_http_init_request. in this function, the Read event is set to ngx_http_process_request_line, that is, the next network event is executed by ngx_http_process_request_line. From the ngx_http_process_request_line function name, we can see that this is to process the request line. As mentioned earlier, the first thing to process the request is to process the request line. Use ngx_http_read_request_header to read request data. Then, call the ngx_http_parse_request_line function to parse the request line. To improve efficiency, nginx uses a state machine to parse request rows. In addition, when comparing methods, it does not directly use string comparison. Instead, it converts four characters into an integer, then compare the number of cpu commands to reduce the number of cpu commands. Many people may know that a request line contains the request method uri and version, but they do not know that the request line can contain the host. For example, a request for a GET http://www.taobao.com/uri HTTP/1.0 such a request line is also valid, and the host is www.taobao.com, at this time, nginx will ignore the host domain in the request header, in the request line, the VM is queried. In addition, for httbench 9, the request header is not supported, so special processing is required here. Therefore, when the request header is parsed later, the protocol versions are 1.0 or 1.1. The parameters parsed by the entire request line are saved to the ngx_http_request_t structure.

After the request line is parsed, nginx sets the handler of the Read event to ngx_http_process_request_headers, and then the subsequent requests are read and parsed in ngx_http_process_request_headers. The ngx_http_process_request_headers function is used to read the request header. like the request line, you still call ngx_http_read_request_header to read the request header and call ngx_http_parse_header_line to parse a line of request headers, the parsed request header is saved to headers_in, the ngx_http_request_t domain. headers_in is a linked list structure that stores all request headers. Some HTTP requests need to be specially processed. these request headers and request processing functions are stored in a ing table, that is, ngx_http_headers_in. during initialization, a hash table is generated, after each request header is parsed, it is first searched in the hash table. if any, the corresponding processing function is called to process the request header. For example, the processing function of the Host header is ngx_http_process_host.

When nginx parses two carriage return line breaks, it indicates the end of the request header. at this time, ngx_http_process_request is called to process the request. Ngx_http_process_request sets the read/write Event Processing function of the current connection to ngx_http_request_handler, and then calls ngx_http_handler to process a complete http request. It may be strange here that the read and write event handler functions are both ngx_http_request_handler. In fact, in this function, the read_event_handler or write_event_handler in ngx_http_request_t will be called based on whether the current event is a read event or. At this time, our request header has been read. as we have said before, nginx does not read the request body first. Therefore, we set read_event_handler to ngx_http_block_reading, that is, no data is read. As mentioned earlier, the actual start of data processing is in the ngx_http_handler function, which sets write_event_handler to ngx_http_core_run_phases and executes the ngx_http_core_run_phases function. Ngx_http_core_run_phases: This function will execute multi-stage request processing. nginx divides the processing of an http request into multiple stages, so this function is to execute these stages to generate data. Because ngx_http_core_run_phases will generate data at the end, we can easily understand why the processing function for writing events is ngx_http_core_run_phases. Here, I will briefly explain the call logic of the function. we need to understand that ngx_http_core_run_phases is called to process the request, and the generated response header will be placed in headers_out of ngx_http_request_t, I will talk about it in the request processing process. In various stages of nginx, requests are processed, and filters are called to filter data and process the data, such as truncked transmission and gzip compression. The filters here include header filter and body filter, that is, processing the response header or response body. Filter is a linked list structure with header filter and body filter. first, all filters in header filter are executed, and then all filters in body filter are executed. In the last filter of the header filter, that is, ngx_http_header_filter, this filter will traverse all the response headers, and finally the response header to be output in a continuous memory, and then call ngx_http_write_filter for output. Ngx_http_write_filter is the last one in the body filter. Therefore, nginx first sets the body Information. after a series of body filters, nginx will also call ngx_http_write_filter for output (as shown in the figure below ).

Note that nginx will place the entire request header in a buffer. the buffer size is set by the client_header_buffer_size configuration item. if the request header is too large, the buffer cannot be loaded, then nginx will re-allocate a new larger buffer to load the request header. this large buffer can be set through large_client_header_buffers. this large_buffer is a set of buffer, such as 4 8 k, it indicates that four 8 K buffers can be used. Note: In order to save the integrity of the request line or header, a complete request line or request header must be placed in a continuous memory. Therefore, a complete request line or request header is required, it is saved in only one buffer. In this way, if the request row is larger than the buffer size, error 414 will be returned. if the size of a request header is greater than the buffer size, error 400 will be returned. After understanding the values of these parameters and the actual nginx practices, in the application scenario, we need to adjust these parameters according to actual needs to optimize our program.

Processing flowchart:

These are the lifecycle of an http request in nginx. Let's look at some concepts related to requests.

The above describes the request in nginx, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.