首页 > 系统相关 >nginx 不转发 header 问题

nginx 不转发 header 问题

时间:2022-12-05 22:00:30浏览次数:72  
标签:http log header 转发 underscores nginx error

之前在一台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;
        }
    }
}

调查

查看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;

分析

需要看看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

标签:http,log,header,转发,underscores,nginx,error
From: https://www.cnblogs.com/Star-Haitian/p/16953672.html

相关文章

  • nginx412常见错误porxy_ssl_name
    Nginx反向代理问题汇总首页Nginx反向代理问题汇总  7月18,2022Nginx反向代理问题汇总作者 Zgao 在安全运维使用Nginx反向代理中出现......
  • linux中nginx的安装
    【是什么】  nginx是一款高性能的http服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。  由俄罗斯的程序设计师lgorSysoev所开发,官方测试nginx能够支撑5......
  • 常用nginx变量以及判断
    当rewrite的重写规则满足不了需求时,比如需要判断当文件不存在时、当路径包含xx时等条件,则需要用到ifif语法if(表达式){...}表达式语法:1、正则表达式匹配:==:等值比......
  • 深入浅出学习透析Nginx服务器的基本原理和配置指南「初级实践篇 」
    什么是Nginx?Nginx(EngineX)是一个轻量级的Web服务器、反向代理服务器及电子邮件(IMAP/POP3)代理服务器、高性能的HTTP服务器,它以高稳定性、丰富的功能集、示例配置文件......
  • 2022年超详细在CentOS 7上安装Nginx方法(源码安装)
    1、下载http://nginx.org/download/nginx-1.13.0.tar.gz2、上传到虚拟机中3、解压tar-zxvfnginx-1.13.0.tar.gz4、删除安装包rm-rfnginx-1.13.0.tar.gz5......
  • 转发和重定向的区别
    转发:request重定向:response问题解析:重定向是浏览器发送请求并收到响应以后再次向一个新地址发送请求,转发是服务器收到请求后,为了完成响应,转到另一个资源(servlet)重定......
  • Request_获取请求参数中文乱码问题处理以及请求转发
    Request_获取请求参数中文乱码问题处理中文乱码问题:get方式:tomcat8已经将get方式乱码问题解决了post方式:会乱码解决:在获取参数前,设置request的编码:r......
  • Nginx之两个前端项目用同一个后台项目,如何配置配置文件?
    Nginx最被人熟知的作用就是反向代理。反向代理:因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据......
  • nginx在windows平台下使用文本编辑器修改配置文件引发的错误
    一、起因最近在使用nginx在windows平台上部署vue,但是使用Windows平台的文本编辑器修改了之后,就会导致错误,打开log日志,查看错误的时候,发现它提示第三行配置出错,死活找不到......
  • centos7安装nginx
    下载解压官网地址:nginx:download下载稳定版中的,Linux版本解压缩tar-zxvfnginx-1.22.1.tar.gz安装依赖yuminstall-yopenssl*&yum-yinstalln......