晨风

-------------------- 业精于勤,荒于嬉;行成于思,毁于随

导航

NGINX代理导致请求头header中的信息丢失问题

Posted on 2020-09-27 13:33  shenyixin  阅读(1524)  评论(0编辑  收藏  举报

问题描述:在请求时往请求头header中放入了签名sign_val信息,在接收请求时再从header中拿出,在本地调试时是可以的,但通过NGINX代理之后发现拿不到。
解决:
1.NGINX代理时加上请求头信息:

  location / 
    {
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_set_header Host  $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://web1;
        }

发现没有解决,最后发现是NGINX对header有所限制,下划线(_)不支持。
方法一:不用下划线
把下划线_改成其他的,如sign_val改成sign-val

方法二:从根本解除nginx的限制
nginx默认request的header的那么中包含’_’时,会自动忽略掉。
解决方法是:在nginx里的nginx.conf配置文件中的http部分中添加如下配置:
underscores_in_headers on; (默认 underscores_in_headers 为off)
我使用的是方法一,方法二没有试。