Home  >  Article  >  Backend Development  >  Nginx output processing process

Nginx output processing process

WBOY
WBOYOriginal
2016-08-08 09:32:281030browse

During the HTTP processing process, we will continue to see several functions. Let’s talk about

      static ngx_int_t ngx_http_echo_handler(ngx_http_request_t *r)
{
       ngx_int_t rc;
       ngx_buf_t *b; 
       //ngx_chain_t是关于ngx_buf_t的一个链表
       ngx_chain_t out; 
       ngx_http_echo_loc_conf_t *elcf; //读取我们写在nginx.conf文件中的配置信息
       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我们可以认为是响应头部的处理
       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; 
        } 
       //设置ngx_chain_t中的ngx_buf_t链表,next为NULL则说明该链表结束
       out.buf = b; 
       out.next = NULL; 
       //设置缓存内容
       b->pos = elcf->ed.data; 
       b->last = elcf->ed.data + (elcf->ed.len);
       b->memory = 1;
       //当前ngx_buf_t已经是ngx_chain_t链表中的最后一项 
       b->last_buf = 1; 
       rc = ngx_http_send_header(r); 
       if(rc != NGX_OK) 
       {
          return rc; 
        } 
     return ngx_http_output_filter(r, &out);
} 
first. Just like what appears in our code, now 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);
 }
. The implementation of this code is not straightforward. It is a sending function, but the request header is processed according to the ngx_http_top_header_filter chain list of the filter module. Next 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;
}
can see that ngx_http_output_filter finally calls the ngx_http_top_body_filter function, which uses There are two parameters, one is the ngx_http_request_t request and the other is the ngx_chain_t linked list. In fact, the passed ngx_chain_t is added to the tail of the request out (also of the ngx_chain_t data type), and its content is added to the tail when outputting. Such a page is output



The above introduces the Nginx output processing process, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:xcache speeds up phpNext article:xcache speeds up php