最近遇到一个 Nginx 转发的坑,一个请求转发到 Tomcat 时发现有几个 http header 始终获取不到,导致线上出现 bug,运维说不是他的问题,这个锅我背了。

自定义header 是这样的:

  • hx_sign

反复检查代码,确定这些 header 是传了的,而且本地测试单独在 tomcat 中是可以接受到这些参数的,所以 tomcat 和命名本身是没问题的,初步断定是 Nginx 的问题。

经过一翻搜索,终于找到了一个 Nginx 的配置参数:underscores_in_headers,这个参数默认值为:off,即默认忽略带下划线的 header。

解决方案:

1、在 http 或者 server 配置中把 underscores_in_headers 配置参数开关打开:

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

增加配置后,然后重启 Nginx。

2、使用破折号(-)代替下划线(_),或者统一规范直接不要使用下划线;

我们来看下一般的 http header 长什么样的:

User-Agent

 

一般所见的 headers 确实也都是中杠线,没有下划线。

Nginx 为什么默认忽略带下划线 header?

我找到了 Nginx 的官方说明:

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/?highlight=underscores#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.

根据官方说明,这样做是为了避免把 headers 映射为 CGI 变量时出现歧义,因为破折号和下划线都会被映射为下划线,所以两者不好区分……

好吧,终于弄清楚了,这个问题也太变态了,这应该是 Nginx 设计时的一个缺陷吧,这个坑我替你们踩了!

所以,推荐大家使用第二种方案吧,统一规范 headers 不要使用下划线,使用 Nginx 默认的配置即可,这样可以尽量避免环境上的差异,以免后续带来问题。

  1. My spouse and i have been really lucky that Louis managed to finish off his research through your precious recommendations he made through your web pages. It is now and again perplexing to just find yourself handing out steps which people today may have been selling. Therefore we know we need the writer to give thanks to for that. The specific illustrations you have made, the simple web site navigation, the relationships you will help to instill – it’s got most powerful, and it’s really leading our son in addition to the family reason why the subject is fun, which is really pressing. Thank you for the whole lot!

  2. My husband and i felt now excited Emmanuel could conclude his inquiry from your ideas he gained in your site. It’s not at all simplistic just to choose to be giving for free ideas that most people have been making money from. And we all do understand we have the website owner to be grateful to because of that. The explanations you’ve made, the easy site menu, the friendships you aid to create – it’s all astounding, and it’s aiding our son and our family consider that that concept is awesome, and that’s exceptionally fundamental. Many thanks for all!

  3. I simply wanted to thank you very much once more. I do not know what I would have tried without these tactics shared by you regarding that subject matter. It was actually an absolute scary problem in my position, nevertheless taking note of this specialized tactic you solved the issue took me to weep with happiness. Now i’m thankful for your support and thus sincerely hope you really know what an amazing job you have been putting in training the mediocre ones via your webblog. Most probably you haven’t met any of us.

  4. I precisely desired to say thanks again. I’m not certain the things I could possibly have carried out in the absence of the concepts discussed by you concerning my area of interest. Entirely was a daunting circumstance for me, but coming across a expert avenue you handled it forced me to cry for contentment. I’m grateful for the assistance and as well , expect you comprehend what a powerful job that you are carrying out educating many people by way of a site. I am certain you’ve never come across all of us.

  5. Thanks for your whole work on this website. My niece takes pleasure in working on research and it is simple to grasp why. We all notice all relating to the powerful ways you render valuable thoughts via your website and in addition recommend response from the others on this subject then our own princess is undoubtedly studying a whole lot. Take pleasure in the rest of the year. You have been performing a terrific job.

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注