Express.js-运行服务器3小时后未发送Cookie

时间:2018-07-02 17:21:00

标签: node.js express nginx cookies ubuntu-16.04

我遇到了一个有趣的问题,我不知道如何解决。

我用基本的身份验证后端构建了一个简单的网站,部署该网站后一切正常。

数据库运行正常,身份验证中间件按其应有的方式工作,但是突然间,当访问该网站时,我尝试登录几个小时后,我什至没有任何cookie就被重定向了。 (我在多个设备上尝试过) 唯一有效的方法是重新启动服务器进程。

使用的技术:

  • 服务器操作系统:Ubuntu 16.04。
  • HTTP服务器:Nginx(用作Node.js应用程序的反向代理)
  • 流程经理:pm2
  • 数据库:MongoDB
  • 速成版:4.16.3。

我的登录页面代码:

app.post("/login", (req, res) => {
if (req.body.email && req.body.password){

  var email     = req.body.email;
  var password  = req.body.password;

  User.findOne({email})
  .then(user =>{
    if (!user) throw new Error("No user found.");
    bcrypt.compare(password, user.password)
    .then(correctPwd =>{
      if (!correctPwd) throw new Error("Wrong password.");
      return user.generateAuthToken();
    })
    .then(token => {


      // The problematic part

      res.cookie("auth", token, authCookie)
      .redirect(redirect); // redirect is a variable passed by a function


    })
    .catch((err)=>{
      res.status(400).redirect(redirect);
    })
  })
  .catch((err)=>{
    res.clearCookie("auth").status(400).redirect(redirect);
  });
} else res.clearCookie("auth").status(400).redirect(redirect);
});

我的Nginx配置:

location / {
    proxy_pass http://localhost:8080/;
    proxy_pass_header Set-Cookie;
    proxy_pass_header P3P;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

0 个答案:

没有答案