最近遇到一个 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.

  6. Awesome write-up. I am a regular visitor of your blog and appreciate you taking the time to maintain the nice site. I will be a frequent visitor for a really long time. Prudence Wain Denoting

  7. So in your price list/sheet, tell the customer what to limit is. A deeper look will indicate taken into consideration they is. That is the percentage you add to the base total of delivery. Glyn Alva Pimbley

  8. we are a company located in humboldt county CA we have a large selection of top shelf marijuana strains, we also have vapes, edibles and more. Our customers are our top priority, please contact us if you have questions like where to buy cannabis online in restricted states/countries. Jodi Ade Pfeifer

  9. autolike, Status Auto Liker, Autolike, auto like, Autoliker, Autolike International, Auto Like, Working Auto Liker, autoliker, Auto Liker, auto liker, Status Liker, Photo Auto Liker, Autoliker, Photo Liker, ZFN Liker, Increase Likes Nedda Dom Constantin

  10. Thanks for your concepts. One thing we’ve noticed is banks along with financial institutions are aware of the spending practices of consumers while also understand that most of the people max out and about their real credit cards around the getaways. They properly take advantage of this specific fact and commence flooding your current inbox along with snail-mail box using hundreds of no-interest APR card offers just after the holiday season concludes. Knowing that should you be like 98% of American community, you’ll rush at the possible opportunity to consolidate card debt and shift balances for 0 apr interest rates credit cards. jjjihkm https://stomachmedi.com – over counter medicine for stomach pain

  11. Does your site have a contact page? I’m having trouble locating it but, I’d like to shoot you an e-mail. I’ve got some ideas for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it expand over time.

  12. We wish to thank you again for the gorgeous ideas you offered Jeremy when preparing her own post-graduate research as well as, most importantly, with regard to providing all the ideas in one blog post. If we had been aware of your website a year ago, we will have been saved the pointless measures we were taking. Thanks to you.

  13. This is the perfect blog for anybody who would like to understand this topic. You understand so much its almost hard to argue with you (not that I personally will need to…HaHa). You certainly put a fresh spin on a topic that has been discussed for many years. Wonderful stuff, just wonderful!

  14. We would like to thank you yet again for the wonderful ideas you gave Janet when preparing her own post-graduate research and also, most importantly, with regard to providing all the ideas within a blog post. In case we had known of your web site a year ago, we’d have been kept from the pointless measures we were employing. Thank you very much.

  15. Hello there, just became alert to your blog through Google, and found that it is really informative. I am going to watch out for brussels. I will appreciate if you continue this in future. Lots of people will be benefited from your writing. Cheers!

  16. We would like to thank you just as before for the lovely ideas you gave Janet when preparing her own post-graduate research plus, most importantly, with regard to providing all the ideas within a blog post. Provided that we had known of your web site a year ago, we might have been saved the pointless measures we were implementing. Thank you very much.

  17. Hello! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share. Many thanks!

  18. Right here is the perfect site for anyone who would like to find out about this topic. You know so much its almost tough to argue with you (not that I really will need to…HaHa). You certainly put a fresh spin on a topic which has been discussed for years. Wonderful stuff, just wonderful!

  19. Does your website have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it expand over time.

  20. Hi there, just became alert to your blog through Google, and found that it is truly informative. I?m gonna watch out for brussels. I?ll appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  21. Hi there, just became aware of your blog through Google, and found that it is truly informative. I?m going to watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!

  22. hello there and thank you for your info ? I have definitely picked up something new from right here. I did however expertise some technical points using this web site, as I experienced to reload the site many times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and can damage your high quality score if advertising and marketing with Adwords. Well I?m adding this RSS to my email and can look out for much more of your respective fascinating content. Ensure that you update this again very soon..

  23. Hello there, just became alert to your blog through Google, and found that it is truly informative. I am going to watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!

  24. Right here is the right blog for anyone who wishes to find out about this topic. You realize a whole lot its almost hard to argue with you (not that I personally would want to…HaHa). You certainly put a brand new spin on a subject that has been discussed for years. Great stuff, just excellent!

  25. It’s perfect time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or tips. Perhaps you could write next articles referring to this article. I desire to read even more things about it!

  26. It is perfect time to make some plans for the future and it’s time to be happy. I have read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you can write next articles referring to this article. I want to read even more things about it!

  27. Does your site have a contact page? I’m having a tough time locating it but, I’d like to send you an email. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it develop over time.

  28. Hello there, just became aware of your blog through Google, and found that it is truly informative. I?m going to watch out for brussels. I?ll appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  29. It is appropriate time to make some plans for the future and it is time to be happy. I have read this post and if I could I desire to suggest you few interesting things or advice. Maybe you can write next articles referring to this article. I desire to read more things about it!

  30. Right here is the perfect web site for anybody who hopes to find out about this topic. You know a whole lot its almost hard to argue with you (not that I actually will need to…HaHa). You certainly put a new spin on a topic that’s been discussed for many years. Excellent stuff, just great!

Leave a Reply

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