Poison

Underscores in HTTP Header Names

关于 HTTP 请求中的 header 键的名称,如果打开 chrome 的调试控制台,可以观察到大部分 HTTP header 的键的名称都使用 - 进行分隔,其规范在 RFC 2616 4.2 Message Headers 中指向的 RFC 822 3.1.2 STRUCTURE OF HEADER FIELDS 中进行了说明:

The field-name must be composed of printable ASCII characters (i.e., characters that have values between 33. and 126., decimal, except colon).

文档表示 ASCII 值在 33 至 126 的字符(除了冒号)都是合法的,但是在之前的一次问题排查中,花了不少时间来定位 header 丢失的问题,最终定位是中间的 NGINX 代理服务器将含有下划线的 header 丢掉了,其中 NGINX 的配置为 Module ngx_http_core_module,默认会丢掉含有下划线的 HTTP header,关于该问题的描述如下:

Missing (disappearing) HTTP Headers

If you do not explicitly set underscores_in_headers on;, NGINX will silently drop HTTP headers with underscores (which are perfectly valid according to the HTTP standard). This is done in order to prevent ambiguities when mapping headers to CGI variables as both dashes and underscores are mapped to underscores during that process.

记录该问题是因为今天同事又遇到了,说接收不到三方平台推送的 header,我排查后发现又是中间的 NGINX 代理服务器使用的默认配置导致含有下划线的 HTTP header 键被丢弃了。

comments

Reference

Why do HTTP servers forbid underscores in HTTP header names - Stack Overflow