Nginx配置项underscores_in_headers

full nginx logo

遇到一个比较奇怪的问题,发现经过Nginx反向代理到服务器端时部分头字段被过滤掉了。经过查阅资料,得知HTTP头字段名称在标准下是使用中划线拼接的,例如

【国内直连ChatGPT 29元起】
国内直连ChatGPT,Plus会员每月29元起,支持最新o1模型探索更多领域,无需注册OpenAI账号。

Custom-Header: QTTC

假如你想使用下划线拼接

Custom_Header: QTTC

这是不符合HTTP标准的,默认在Nginx下进行会被drop掉,担心跟CGI变量混淆,以下是官方说明

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.

所以如果需要使用下划线拼接的头字段,就需要明确声明underscores_in_headers在配置文件里

nginx.conf

...
server {
  ...
  underscores_in_headers on;
  ...
}

然后重启一下Nginx即可生效。

分享

TITLE: Nginx配置项underscores_in_headers

LINK: https://www.qttc.net/540-nginx-underscores-in-headers.html

NOTE: 原创内容,转载请注明出自琼台博客