nginx不转发http header问题解决

文章整理自网络-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/78199977,建议读者阅读原文,确保获得完整的信息

1.问题

之前在一台ECS上搭了个nginx搞跳转服务,昨天突然发现header中自定义数据一直获取不到。因为利用nginx跳转时读取header异常,不用nginx时读取header正常。

先来看看nginx.conf配置文件:

worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  http://11.239.162.48;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://11.239.162.48:7000;
            client_max_body_size  100m;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

2.调查

查看nginx的log日志时,也没有明显的异常记录。查了下网络上的相关信息,有以下两点:

1、默认的情况下nginx引用header变量时不能使用带下划线的变量。要解决这样的问题只能单独配置underscores_in_headers on;
2、默认的情况下会忽略掉带下划线的变量。要解决这个需要配置ignore_invalid_headers off。

查看自己header中自定义的变量时,有一个project_id包含下划线,接口处理时一直获取不到其值,所以提示异常。

不过,我仅将underscores_in_headers on配置信息加到http块中去了,业务恢复正常。

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    underscores_in_headers on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

3.分析

需要看看nginx的一段源码,其中有这么一个片段:

ngx_http_parse_header_line(ngx_http_request_t *r, ngx_buf_t *b,ngx_uint_t allow_underscores)

if (ch == '_') {
    if (allow_underscores) {
        hash = ngx_hash(0, ch);
        r->lowcase_header[0] = ch;
        i = 1;
    } else {
        r->invalid_header = 1;
    }
     break;
}

即包含一个关键变量:allow_underscores,是否允许下划线。

原来nginx对header name的字符做了限制,默认 underscores_in_headers 为off,表示如果header name中包含下划线,则忽略掉。而我的自定义header中恰巧有下划线变量。

所以,后续再碰到类似情况,要么header中自定义变量名不要用下划线,要么在nginx.conf中加上underscores_in_headers on配置。

源码地址为:https://trac.nginx.org/nginx/browser/nginx/src/http/ngx_http_parse.c

这里写图片描述

4.引用

1、http://blog.csdn.net/wx_mdq/article/details/10466891

2、https://trac.nginx.org/nginx/browser/nginx/src/http/ngx_http_parse.c

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值