nginx默认过滤带下划线header

乡下的树 2021年06月15日 350次浏览

问题

开发反馈,有个第三方通知接口,本地直接运行程序可以正常收到消息,但是上线生产环境后,一直无法成功收到消息。
请求信息如截图:
9079faacbf463a8e75545551b5a23bd

原因

第三方请求进来,需要通过nginx反向代理到后端程序,自定义Header中,参数action_name带下划线_。

查询nginx源码,header name的字符做了限制,默认 underscores_in_headers 为off,表示如果header name中包含下划线,则忽略掉,所以导致上线生产环境后获取不到消息。

源码部分:


rc = ngx_http_parse_header_line(r, r->header_in, cscf->underscores_in_headers); 
if (r->invalid_header && cscf->ignore_invalid_headers) 

在ngx_http_parse_header_line() 函数中 
if (ch == ‘_’) { 
                if (allow_underscores) { 
                    hash = ngx_hash(hash, ch); 
                    r->lowcase_header[i++] = ch; 
                    i &= (NGX_HTTP_LC_HEADER_LEN – 1); 

                } else { 
                    r->invalid_header = 1; 
                } 

解决

  1. nginx.conf配置中http部分, 增加underscores_in_headers on;
  2. 可用减号-替代下划线符号_
    clipboard-1649405155680