>  기사  >  백엔드 개발  >  Nginx 출력 처리 프로세스

Nginx 출력 처리 프로세스

WBOY
WBOY원래의
2016-08-08 09:32:281028검색

HTTP 처리 과정에서 코드에 나타나는 것과 마찬가지로 여러 함수에 대해 먼저 살펴보겠습니다

      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);
} 

이제 두 함수를 구문 분석합니다. 그 중 하나는 ngx_http_send_request입니다. 다음

 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);
 }
이 코드는 송신 기능을 직접 구현하지 않고 필터 모듈의 ngx_http_top_header_filter 연결 목록에 따라 요청 헤더를 처리합니다. 다음 단계는 ngx_http_output_filter 함수입니다.

함수는 다음과 같이 정의됩니다.

 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;
}
ngx_http_output_filter가 마침내 두 개의 매개변수를 사용하는 ngx_http_top_body_filter 함수를 호출하는 것을 볼 수 있습니다. 하나는 ngx_http_request_t 요청이고 다른 하나는 ngx_chain_t 연결 목록입니다. 전달된 ngx_chain_t는 요청된 출력(ngx_chain_t 데이터 유형이기도 함)에 추가되며 출력 시 해당 내용을 꼬리에 추가합니다. 이런 페이지가 출력됩니다

위 내용은 다양한 측면을 포함하여 Nginx 출력 처리 과정을 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.