文章19:Nginx中为http请求或响应添加头部字段
Aug 08, 2016 am 09:19 AM欢迎转载 转载请注明出处http://blog.csdn.net/yankai0219/article/details/8270219
0.序1.upload模块中添加头部字段 2.Nginx对于ngx_http_headers_out_t headers_out.headers成员变量的处理 1)请求: 2)响应: 3.向请求或者响应中添加头部字段 1)程序 在请求中添加头部字段 helloHeaders 内容123344 |
0.序
根据项目需要,需要向http响应中添加文件的md5头部字段。众所周知,Nginx的响应的头部字段都在
通过查阅文章,我们可以发现http协议支持md5头部字段,但是nginx在
那么Nginx是不是真的不支持md5头部字段呢?通过查阅发现upload模块支持content-md5头部字段。那么说明nginx是支持content-md5,那么我们如何实现呢?
综上所述:1)http协议支持md5头部字段 2)Nginx支持content-md5
1.upload模块中添加头部字段
在nginx upload模块中通过
在Nginx upload模块源码中可以看到ngx_http_upload_add_headers函数。
其过程为:1)解析ulcf->header_templates中的头部字段
2)在
3)为该新添加的元素填充值。
static ngx_int_t ngx_http_upload_add_headers(ngx_http_request_t *r, ngx_http_upload_loc_conf_t *ulcf) { /* {{{ */ ............... h = ngx_list_push(&r->headers_out.headers);
//在
/*接下来,就是为该新添加的元素填充值*/ 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对于
1)请求:
在ngx_http_proxy_create_request函数中,我们可以看到一个http请求的构建(包括请求行、请求头、请求体三部分),我们只需要看一下对请求头的处理即可。
if (plcf->upstream.pass_request_headers) {
part = &r->headers_in.headers.part;
header = part->elts;
for (i
= 0; /* void */ ; i++) {
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++ = ':' ;
*b->last++ = ' ' ;
b->last = ngx_copy(b->last, header[i].value.data,
header[i].value.len);
*b->last++ = CR; *b->last++ = LF;
...
}
}
|
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中
void yk_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 =
name.data;
h-> value.len =
value.len;
h-> value.data =
value.data;
}
#endif
|
由于我是设置代理,因此所有的请求都会进入ngx_http_proxy_handler函数,因此在ngx_http_proxy_handler函数刚开始处,加入 yk_add_headers_in( r),那么所有的请求头部中都会有helloHeaders头部字段。通过抓包可以看到的确如此。
以上就介绍了文章19:Nginx中为http请求或响应添加头部字段,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Solution : Votre organisation vous demande de modifier votre code PIN

Comment ajuster les paramètres de bordure de fenêtre sous Windows 11 : modifier la couleur et la taille

10 façons de régler la luminosité sous Windows 11

Comment désactiver l'authentification de navigation privée pour iPhone dans Safari ?

Que signifie le code d'état http 520 ?

Le script d'activation numérique Win10/11 MAS version 2.2 prend à nouveau en charge l'activation numérique
![Comment masquer et afficher des dossiers sous Windows 11 [3 façons]](https://img.php.cn/upload/article/000/887/227/169542942766265.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Comment masquer et afficher des dossiers sous Windows 11 [3 façons]

Interface de sortie audio numérique sur la carte mère-SPDIF OUT
