首页 > 解决方案 > 如何使我的所有标题元素通过 nginx 到我的节点 js api

问题描述

我正在从我的 vue js 应用程序发送一个带有标题的请求

{
   Access-Control-Allow-Headers: "Origin, Content-Type, X-Auth-Token"
   Access-Control-Allow-Methods: "GET, POST, PATCH, PUT, DELETE, OPTIONS"
   Access-Control-Allow-Origin: "*"
   hotel_id: "custom-hotel-id-value-here"
   token: "custom-token-value-here"
}

当它到达我的 digitalocean 液滴时,它会通过NGINX。在 node-express api 上记录请求标头时,它显示了另一种没有“hotel_id”元素的标头格式,但我确实需要该标头元素来继续系统的适当功能。这是在 node-express api 中记录的请求标头

{
   'x-real-ip': '197.157.*.*',
   'x-forwarded-for': '197.157.*.*',
   'x-nginx-proxy': 'true',
   host: '206.189.*.*:*',
   connection: 'close',
   'access-control-allow-origin': '*',
   'access-control-allow-headers': 'Origin, Content-Type, X-Auth-Token',
   'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
   token: 'custom-token-value-here'
   'access-control-allow-methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
   accept: '*/*',
   origin: 'http://206.189.*.*',
   referer: 'http://206.189.*.*/',
   'accept-encoding': 'gzip, deflate',
   'accept-language': 'en-US,en;q=0.9'
}

在部署之前,它在本地计算机上运行良好。

标签: node.jsexpressnginxnginx-reverse-proxyrequest-headers

解决方案


经过我的研究,我发现标题中的下划线不能通过 nginx,对于这个例子,它是'hotel_id'。我需要添加

underscores_in_headers on;

在我的 nginx 服务器块(上下文:http,服务器)中,因为它的默认值是

underscores_in_headers off;

有关更多信息,请点击此链接 https://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers


推荐阅读