【问题标题】:Nginx Unable To Pas Custom Header To ProxyNginx 无法将自定义标头传递给代理
【发布时间】:2020-07-15 19:05:14
【问题描述】:

我正在运行 Nginx/1.17.5,并希望将带有下划线的自定义标头传递给代理。

如果从标题中删除下划线(x-test而不是x_test),则标题通过。

这里是配置

server {
    listen 80 default_server;
    access_log /dev/stdout main;

    location /ping {
        return 200 'pong';
    }

    proxy_pass_request_headers on;
    underscores_in_headers on;

    location / {
        proxy_pass https://example.com:443;
        add_header x_test $http_x_test always;
    }
}

作品

curl -X POST 'https://example.com/upstream-app/ \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'x-test: 7359af9c-a191-41b7-aeab-a3389e1c5e1e' \
--data '{
"body": "VALID",
"from": "99999",
}'

失败

curl -X POST 'https://example.com/upstream-app/ \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'x_test: 7359af9c-a191-41b7-aeab-a3389e1c5e1e' \
--data '{
"body": "VALID",
"from": "99999",
}'

对此的任何想法/指针将不胜感激。

【问题讨论】:

    标签: nginx nginx-reverse-proxy


    【解决方案1】:

    为此,您必须使用ngx_stream_core_module 来使用 tpc 负载平衡

    由于您打算对后端服务器使用 SSL 终止,而不是在面向 Internet 的负载平衡器中,因此您必须使用 SSL-passthrough,这可以通过流模块完成。

    试试这个:

    user  nginx;
    worker_processes  1;
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
      upstream example-http {
        server example.com:80;
      }
      server {
        listen 80 default_server;
        access_log /dev/stdout main;
    
        location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header Host $http_host;
          proxy_set_header X-Forwarded-Host $server_name;
          proxy_set_header Connection "";
          add_header    x_test $http_x_test always;
          proxy_redirect     off;
          proxy_connect_timeout  60;
          proxy_http_version 1.1;
          proxy_pass_request_headers on;
          underscores_in_headers on;
          proxy_pass http://example-http$request_uri;
        }
        location /ping {
            return 200 'pong';
        }
      }
    }
    stream {
      upstream example-https {
        server example.com:443;
      }
    
      server {
        proxy_protocol on;
        tcp_nodelay on;
        listen 443;
        proxy_pass example-https;
      }
    }
    

    【讨论】:

      【解决方案2】:

      #修复

      将 proxy.conf 更新为

      server {
          listen 80 default_server;
          access_log /dev/stdout main;
      
          location /ping {
              return 200 'pong';
          }
      
          underscores_in_headers on;
      
          location / {
              proxy_pass https://example.com:443;
              proxy_pass_request_headers on;
          }
      }
      

      然后在 Docker 容器上安装 TcpDump 并运行以下命令以检查所有 GET 和 POST 标头,并根据正则表达式包括带下划线的标头。注意下面的命令假设 Docker 在端口 80 上运行,设置为正确的端口号。

      stdbuf -oL -eL /usr/sbin/tcpdump -A -s 10240 "tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)" | egrep -a --line-buffered ".+(GET |HTTP\/|POST )|^[_A-Za-z0-9-]+: " | perl -nle 'BEGIN{$|=1} { s/.*?(GET |HTTP\/[0-9.]* |POST )/\n$1/g; print }'
      

      【讨论】:

        猜你喜欢
        • 2021-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-10
        • 2016-06-21
        • 2012-11-17
        • 1970-01-01
        相关资源
        最近更新 更多