Nginx output process

Source: Internet
Author: User
: This article mainly introduces the Nginx output processing process. if you are interested in the PHP Tutorial, refer to it. In the HTTP processing process, we will constantly see several functions first.
Static ngx_int_t records (records * r) {ngx_int_t rc; ngx_buf_t * B; // ngx_chain_t is a linked list ngx_buf_t ngx_chain_t out; records * elcf; // read the records written in nginx. elcf = ngx_http_get_module_loc_conf (r, ngx_http_echo_module); if (! (R-> method & (NGX_HTTP_HEAD | NGX_HTTP_GET | NGX_HTTP_POST) {return NGX_HTTP_NOT_ALLOWED ;} // headers_out can be considered as the response header processing r-> headers_out.content_type.len = sizeof ("text/html")-1; r-> headers_out.content_type.data = (u_char *) "text/html"; r-> headers_out.status = NGX_HTTP_ OK; r-> headers_out.content_length_n = elcf-> ed. len; if (r-> method = NGX_HTTP_HEAD) {rc = ngx_http_send_header (r); if (rc! = NGX_ OK) {return rc;} B = ngx_pcalloc (r-> pool, sizeof (ngx_buf_t); if (B = NULL) {ngx_log_error (NGX_LOG_ERR, r-> connection-> log, 0, "Failed to allocate response buffer. "); return NGX_HTTP_INTERNAL_SERVER_ERROR;} // sets the ngx_buf_t linked list in ngx_chain_t. if next is NULL, it indicates that the chain table ends out. buf = B; out. next = NULL; // Set cache content B-> pos = elcf-> ed. data; B-> last = elcf-> ed. data + (elcf-> ed. len); B-> memory = 1; // The current ngx_buf_t has Is the last B-> last_buf = 1; rc = ngx_http_send_header (r); if (rc! = NGX_ OK) {return rc;} return ngx_http_output_filter (r, & out );}

As shown in our code, parse two functions, one of which is ngx_http_send_request. the code is as follows:

 ngx_http_send_header(ngx_http_request_t *r) {     if (r->header_sent) {         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,                       "header already sent");         return NGX_ERROR;     }      if (r->err_status) {         r->headers_out.status = r->err_status;         r->headers_out.status_line.len = 0;     }      return ngx_http_top_header_filter(r); }
This code does not directly implement the sending function. Instead, it processes the request header based on the ngx_http_top_header_filter linked list of the filter module. The following is the ngx_http_output_filter function,

The function is defined as follows:

 ngx_int_t ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in) {     ngx_int_t          rc;     ngx_connection_t  *c;      c = r->connection;      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,                    "http output filter \"%V?%V\"", &r->uri, &r->args);      rc = ngx_http_top_body_filter(r, in);      if (rc == NGX_ERROR) {         /* NGX_ERROR may be returned by any filter */         c->error = 1;     }      return rc;}
You can see that ngx_http_output_filter calls the ngx_http_top_body_filter function. two parameters are used, one is the ngx_http_request_t request and the other is the ngx_chain_t linked list, in fact, it is to add the passed ngx_chain_t to the end of the request (which is also of the ngx_chain_t data type) and add the content to the end of the output line. Such a page is output.


The above describes the Nginx output processing process, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.