Article 19:nginx to add a header field for an HTTP request or response

Source: Internet
Author: User
Please indicate the source of the reproduced reproduced http://blog.csdn.net/yankai0219/article/details/8270219
0. Sequence 1.upload module add Head field 2.Nginx for ngx_http_headers_out_t headers_out.headers member variable processing 1) Request: 2 response: 3. To request or add header field 1 to the response the program adds a header field to the request Helloheaders content 123344


0. PrefaceYou need to add the MD5 header field of the file to the HTTP response, depending on your project needs. As we all know, Nginx's response to the head field is in the ngx_http_headers_out_t structure, and by looking through it we find that there is no MD5 header field.      Is that the HTTP protocol does not support MD5 header fields? By looking at the article, we can find that the HTTP protocol supports the MD5 header field, but Nginx does not add the header field in the ngx_http_headers_out_t structure body. Nginx author of this explanation http://mailman.nginx.org/pipermail/nginx/2008-July/006251.html so nginx is not really do not support MD5 header field. The lookup found that the upload module supports CONTENT-MD5 header fields.      So how do we implement Nginx is to support CONTENT-MD5? To sum up: 1 HTTP protocol support MD5 header field 2) Nginx support CONTENT-MD5 Add Header field in 1.upload module
In the Nginx upload module, save the Header field in the Ngx_http_upload_add_header resolution configuration file to the ULCF-> header_templates, and then by calling Ngx_http_upload_add _headers function, add the header field to the head of the response. Next we analyze how to add to the response head.      The Ngx_http_upload_add_headers function can be seen in the Nginx upload module source code. The process is: 1 parsing the header field in the ULCF-> header_templates 2) headers_out ngx_list_t in ngx_http_headers_out_t hea                       Add an element to the ders. 3 fills the value for the newly added element.
static  ngx_int_t  ngx_http_upload_add_headers (ngx_http_request_t  * R,  ngx_http_upload_loc_ conf_t  * ULCF)   { /* {{* *               &nbsp ... &nbsp.. ;                h  =  Ngx_ List_push (& R-> headers_out. Headers); ngx_list_t           in ngx_http_headers_out_t   headers_out                          * Next, is to fill the value for the newly added element */                  h-> hash  =  1;                  h-> Key. len  =  name. Len;                  H-> key. data  =  name. data;                  h-> value. len  =  value. Len;                  h-> value. data  =  value. data;                 ................ ...}  /*}}} */

2.Nginx Processing of ngx_http_headers_out_t headers_out.headers member variables 1) Request:In the Ngx_http_proxy_create_request function, we can see the construction of an HTTP request (including the request line, the request header, the request body three parts), we only need to look at the request header processing.
If   (plcf->upstream.pass_request_headers) {        part = &r-> Headers_in.headers.part;         Header = part->elts;           for   (i = 0; /* void */  i++) {     &N bsp;       if   (i >= part->nelts) {                  if   (Part->next = NULL) {                      break  ;                }                  part = part->next;                 Header = Part->elts ;                 i = 0;            }               if   (Ngx_hash_find (&plcf->headers_set_hash, Header[i].hash,                                 Header[i].lowcase_key, Header[i].key.len))              {                  Continue  ;            }              b->last = ngx_copy (B->last, Header[i].key.data, Header[i].key.len);             *b->last++ =&NBsp ': '  ; *b->last++ =  '  ;             b->last = ngx_copy (B->last, header[i). Value.data,                                 Header[i].value.len);             *b->last++ = CR; *b->last++ = LF;           &nbsp ...        }    
2) Response:The processing for headers is in the Ngx_http_header_filter_module module Ngx_http_header_filter, and the procedure is similar to the header processing in the request.
3. Add the header field to the request or responseThere are ngx_list_t headers member variables in the member variable headers_in and headers_out in ngx_http_request_t. The function is used to store the header field. Within this function you can see the Ngx_list_push (&r->headers_out.headers) function, which puts the header field in the response. Extrapolate, you can use the Ngx_list_push (&r->headers_in.headers) function to put the header field into the request. 1 The program adds the header field to the request Helloheaders content 123344
This function is located in the Ngx_http_proxy_handler of ngx_http_proxy_module.c
voidyk_add_headers_in(ngx_http_request_t *r) {  /* {{{ */printf("Begin into Yk_add_headers_in");                   ngx_table_elt_t *h;                   ngx_str_t name = ngx_string ("Helloheaders");                  ngx_str_t value = ngx_string ("123344"); h = Ngx_list_push (&r-> headers_in. Headers);if(h = = NULL) { return;                  } h-> hash = 1; H-> key. Len = name.                  Len; H-> key. Data

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.