Nodejs Express Api 仅在生产中获取未定义标头

问题描述 投票:0回答:1

我有这个身份验证中间件,

  const authMiddleware = (req, res, next) => {
  const token = req.header("auth_token");
  console.log(req.header('auth_token'));

  if (!token || token==null) {
    return res.status(401).json({ message: "No token, authorization denied" });
  }else {
 ...
});

现在这在本地主机上运行良好,但在由 nginx 托管的 生产上 此代码不起作用,给出的响应为“没有令牌,授权被拒绝” 和令牌获取为未定义

node.js express nginx header production
1个回答
0
投票

在您的 nginx 设置中,您需要设置

proxy_pass_request_headers
,以便代理将标头转发到您的后端服务器。

location / {
    ...
    proxy_pass_request_headers     on;
}

其次,如果您需要在标题中包含

_
(
auth_token
),则还需要启用它:

underscores_in_headers     on;

否则,您需要使用不带

_
的标头名称。

© www.soinside.com 2019 - 2024. All rights reserved.