Nginx: HTTP header processing

Here Insert Picture Description

A request header

conf-> headers-> hash
the header field proxy_set_header instruction set, and a field designating ngx_http_proxy_headers structure formed hash table lookup for the header field. If the contents of both conflict to proxy_set_header prevail. Customization section, must be sent.
conf-> headers-> lengths: forming nginx script, calculates the length of the header field.
conf-> headers-> values: nginx script form, calculates the header field contents.

  • cmcf-> headers_in_hash
    according ngx_http_headers_in structure (the structure define request header fields, and assigning values of these fields is defined), the hash table is initialized. For requesting the header analysis stage r-> headers_in members initialized.

  • 解析(ngx_http_parse_header_line)

  • Processing (ngx_http_process_request_headers)
    for r-> headers_in assignment. For headers_in_hash can be found stored in the r-> headers_in.headers also need to save r- headers_in particular field>; for finding out, stored directly in the r-> headers_in.headers. For invalid_header, for example, "_" header fields begins, non-alphanumeric header fields begins, whether to save the configuration (ignore_invalid_headers) selection.

  • 发送(u->create_request(ngx_http_proxy_create_request))

    /*首先使用plcf->headers->values脚本,对nginx自定义字段进行发送*/
          e.ip = headers->values->elts;
          e.pos = b->last;
          e.request = r;
          e.flushed = 1;
          le.ip = headers->lengths->elts;
          while (*(uintptr_t *) le.ip) {
          }
          
      /*根据pass_request_headers配置(默认使能),将r->headers_in.headers中保存的原始报文的头字段进行发送,在发送前,首先用变量名查询conf->headers->hash哈希表,如果原始字段和自定义字段冲突,已自定义字段为准*/
          if (plcf->upstream.pass_request_headers) {
           
          }
    

Here Insert Picture Description

Second, the response header

  • 读取(ngx_http_proxy_process_header)
    umcf->headers_in_hash哈希表:ngx_http_upstream_init_main_conf函数会根据ngx_http_upstream_headers_in结构体(该结构体定义一些响应头字段,并定义这些字段的赋值方法),对该哈希表进行初始化。当解析结束后,nginx使用解析到的头字段name查询上述哈希表,并通过相对应的handler方法,对r->upstream->headers_in.xxx赋值。

     hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
                        h->lowcase_key, h->key.len);
      
     if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
         return NGX_ERROR;
     }
    

    给r->upstream->headers_in.headers增加“server”和“date”字段。

  • 解析(ngx_http_parse_header_line)

  • 存储(r->upstream->headers_in.headers(ngx_http_upstream_headers_in_t))

  • Processing (ngx_http_upstream_process_headers)
    the head has been provided to the parsed request headers_out members in the structure ngx_http_request_t.
    U-> conf -> hide_headers_hash : header field proxy_hide_header the instruction set, and a field designating ngx_http_proxy_hide_headers structure to form a hash table, for setting a header field in response to prohibit transmission to the client. However proxy_pass_header specified header field set will be set as an exception, it is excluded from the prohibition.
    If the field is present in umcf-> headers_in_hash, the call hh-> copy_handler method, the content copied to the r-> headers_out. And if the field is not present, then the copy directly to r-> headers_out.headers.

  • Send (ngx_http_header_filter)

Guess you like

Origin blog.csdn.net/u013032097/article/details/91385419