Nginx 輸出處理過程

來源:互聯網
上載者:User
在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添加到請求的out(也是ngx_chain_t資料類型的)的尾部,在進行輸出的時候,將其內容添加到尾部。這樣一個頁面就輸出了


以上就介紹了Nginx 輸出處理過程,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.