【问题标题】:Custom headers not received in nodejs while using nginx reverse proxy使用 nginx 反向代理时,nodejs 中未收到自定义标头
【发布时间】:2018-03-06 01:11:36
【问题描述】:

我有 nodejs 和 nginx 正在运行,我在 API 'api_key' 中发送了一个额外的标头,它没有在 nodejs 中的 req.headersreq.get('api_key') 中收到我有下面的 nginx 配置文件

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    server_name mysite.com;
    return 301 https://$host$request_uri;
 location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:9102/;
    proxy_set_header Host $http_host;
    proxy_set_header api_key $http_api_key; 
    #proxy_set_header api_key 'Some Value'; works 
    proxy_redirect off;
  }
}

如果设置proxy_set_header api_key 'some value' 的值,它会起作用并且标题会打印在控制台上,但api_key 会发生变化,这就是我使用$http_api_key 的原因,以便api_key 中的任何内容都被接收到自定义标题是从休息客户端发送的。我尝试了几种解决方案,例如proxy_set_header api_key $upstream_http_api_key;,但没有帮助。我想在 nodejs 中接收从 rest 客户端发送的任何自定义标头。

【问题讨论】:

  • 添加proxy_pass_header api_key;,不要自己设置。看看有没有帮助

标签: node.js nginx http-headers reverse-proxy nginx-reverse-proxy


【解决方案1】:

默认情况下,nginx 不传递包含下划线的标头。

试试:

underscores_in_headers on;

详情请见this document

或者,使用API-KEYX-API-KEY 代替API_KEY

【讨论】:

  • node.js 服务器是否有类似的配置通过永远运行?这就是我们运行应用程序的方式 - /bin/forever start -o logs/out.log -e logs/err.log app.js
  • 我有类似的问题,我添加了 underscores_in_headers ;但仍未定义...stackoverflow.com/questions/51265803/…
猜你喜欢
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2010-11-03
  • 2021-04-06
  • 2021-12-18
  • 2015-06-20
  • 1970-01-01
相关资源
最近更新 更多