系统相关
首页 > 系统相关> > 解决通过Nginx转发的服务请求头header中含有下划线的key,其值取不到的问题

解决通过Nginx转发的服务请求头header中含有下划线的key,其值取不到的问题

作者:互联网

问题

由于在http请求头的头部中设置了一些自定义字段,刚好这些字段中含有下划线,比如bundle_name这种,后端在获取头部信息时,发现取不到对应的值

 

原因分析

nginx源码

ngx_http_parse_header_line(ngx_http_request_t *r, ngx_buf_t *b,ngx_uint_t allow_underscores)

if (ch == '_') {
    if (allow_underscores) {
        hash = ngx_hash(0, ch);
        r->lowcase_header[0] = ch;
        i = 1;
    } else {
        r->invalid_header = 1;
    }
     break;
}

这里有一个关键变量:allow_underscores,是否允许下划线。

原来nginx对header name的字符做了限制,默认 underscores_in_headers 为off,表示如果header name中包含下划线,则忽略掉。而我的自定义header中恰巧有下划线变量。

 

解决方法

在nginx.conf配置http中加上underscores_in_headers on配置

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    underscores_in_headers on;
    keepalive_timeout  65;
}

 

 

标签:ch,http,下划线,Nginx,underscores,header,key,ngx
来源: https://blog.51cto.com/u_14318784/2812546