nginx不转发http header问题解决

来源:互联网 发布:淘宝买家让加微信骗局 编辑:程序博客网 时间:2024/06/05 23:53

文章整理自网络-作者@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

原创粉丝点击