首頁  >  文章  >  後端開發  >  文章19:Nginx中為http請求或回應添加頭部字段

文章19:Nginx中為http請求或回應添加頭部字段

WBOY
WBOY原創
2016-08-08 09:19:122209瀏覽

歡迎轉載轉載請註明出處http://blog.csdn.net/yankai0219/article/details/8270219

0.序

     依專案需要,需要向http回應中新增檔案的md5頭部欄位。眾所周知,Nginx的回應的頭部欄位都在ngx_http_headers_out_t 結構體中,透過查閱我們發現裡面並沒有md5頭部欄位。那是不是http協定不支援md5頭部字段呢?

     透過查閱文章,我們可以發現http協定支援md5頭部字段,但是nginx在ngx_http_headers_out_tngx_http_headers_out_t

ngx_http_headers_out_t

ng字段。 nginx作者對此的解釋http://mailman.nginx.org/pipermail/nginx/2008-July/006251.html     那麼Nginx是不是真的不支援md5頭部欄位呢?透過查閱發現upload模組支援content-md5頭部欄位。那麼說明nginx是支援content-md5,那我們要如何實現呢?

     綜上所述:1)http協定支援md5頭部欄位 2)Nginx支援content-md51.upload模組中加入頭部欄位在nginx uploadload模組中透過解析設定檔中透過解析組態檔案_c中的頭部欄位儲存到ulcf

->

header_templates中,然後透過呼叫

ngx_http_upload_add_headers函數,將頭部欄位加入到回應的頭部中。接下來我們分析是如何添加到響應頭部中的。 在Nginx upload模組原始碼中可以看到ngx_http_upload_add_headers函數。      其過程為:1)解析ulcf

->

header_templates中的頭部字段        x_http_headers_out_t  headers_out的ngx_list_t headers中加入一個元素 。

                      3)填充其所新增的元素上。

     

0.序1.upload模組中加入頭部欄

headers_out.headers成員變數的處理

            1)請求:

            2)回應:

3.向請求或回應中新增頭部 .

static ngx_int_t ) r, ngx_http_upload_loc_conf_t *ulcf) * {{{ */               ...............          (&r

->

headers_out .headers); //在ngx_http_headers_out_t  headers_out的ngx_list_t                                 /*接下來,就是為該新添加的元素填充值*/                h->hash = 1

;

                

h ->key.len = name

      h->key.data = name.data;

                h->.. ;                 h->

h

-> = .資料;                ................................ }  

/* }}} */

2.Nginx對於ngx_http_headers_out_t  headers_out.headers成員變數的處理

1)請求:

在ngx_http_proxy_create_request函數中,我們可以看到一個http請求的建置(包括請求行、請求頭、請求體三部分),我們只需要三個部分),我們只需要三個部分看一下對請求頭的處理即可。

if (plcf->upstream.pass_request_headers) {🠎 🠎 headers.part;
        header = part- >elts;
        
 for (ifor (i = 0; /* void */ ; i++) {
    (i >= part->nelts) {                >n == NULL) {
                    break ;
                }                part = part->next;
                header = part ->elts;
                i = 0;
            
if (ngx_hash_find(
 (ngx_hash_find(&plcf-ash 是 header[i].hash,
                              header[i].lowcase_key, header[i].key.len))            {                
continue
 ;
            }  header[i].key.data, header[i].key.len);             *b->last++ = ':'
 ':'
; *b->last++ = ' '
 ;    header[i].value.len);            *b->last++ = CR; *b->last++++
        }
    }

2)回應:

對於headers的處理是在ngx_http_header_filter_module模組中的ngx_http_header_filter,其過程與請求中的header處理很類似。

3.在請求或回應中加入頭部欄位

在ngx_http_request_t中成員變數headers_in及headers_out中都有ngx_list_t headers成員變數。其作用用於存儲頭部字段。在這個函數裡面可以看到ngx_list_push(&r->headers_out.headers)函數,將頭部欄位放入回應中。

舉一反三,可以採用ngx_list_push(&r->headers_in.headers)函數,將頭部欄位放入請求中。

1)程序 在請求中加入頭部欄位helloHeaders 內容123344

此函數位於ngx_http_proxy_module.c的ngx_http_proxy_handler

 
 yk_add_headers_in( ngx_http_request_t *r) { /* {{{{ */            "begin into yk_add_headers_in"
 );                               *h;                  = ngx_string(
"helloHeaders" );   ngx_str_t 
value = ngx_string("123344" );   headers_in.
headers );                  if == NULL
){             return ;                    h-> 
hash  = 1;                   = name.len
;       .
data = name.
data;       
.len = value.len;
                 h-> value. value. value.data;}
由於我是設定代理,因此所有的請求都會進入ngx_http_proxy_handler函數,因此在ngx_http_proxy_handler函數剛開始處,加入  yk_add_headers_in
( r),那麼所有的請求頭部都會有所有的請求頭部位中都會有頭部欄位。透過抓包可以看到的確如此。

                以上就介紹了文章19:Nginx中為http請求或回應添加頭部字段,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn