node.jsexpressnginxheaderproduction

Nodejs Express Api Getting Undefined header only on Production


I have this auth middleware,

  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 {
 ...
});

now this working fine on local host, but on production hosted with nginx this code is not working , giving response as "No token, authorization denied" and token getting as undefined


Solution

  • In your nginx setup, you need to set the proxy_pass_request_headers, so that the proxy shall forward the headers to your backend server.

    location / {
        ...
        proxy_pass_request_headers     on;
    }
    

    Second, if you need to have _ in your header (like auth_token header), you need to enable that as well:

    underscores_in_headers     on;
    

    Or else, you need to use a header name without _.