HTTP header 参数丢失情况分析以及解决防范

HTTP header 参数丢失情况分析以及解决防范

1.问题

当项目有Nginx做了代理的时候,请求头中传参 "user_ip" ,接口接收不到

2.问题分析

   默认nginx是不能转发带_的header信息的,为什么不能支持下划线呢,因为nginx的源码中默认判定就是不合法的:
          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;
          }
从if (allow_underscores)中你可以看出nginx对header name的字符做了限制,默认 underscores_in_headers 为off,表示如果header name中包含下划线,则忽略掉。

3.解决办法

1、修改nginx配置

 在nginx 的 http部分添加如下:

underscores_in_headers on; (默认 underscores_in_headers 为off)

2、修改这个字段,取消下划线(建议)

列如 把原来的user_ip 换为  userIp 或者 user-ip         

4.总结

避免这种问题,这里给出如下几点建议:

1.严格规范header中的键值不要带_
2.统一环境,不要QA不用nginx代理,beta/onlien用nginx代理,让测试在QA环境规避了这种问题
3.运维统一线上nginx开启underscores_in_headers配置项:underscores_in_headers on

建议 命名的时候严格规范,不要在参数中带 “_” 。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上善若水-学者至上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值