身份validation问题Node.js与护照和Nginx作为代理服务器

我正在运行一个Node应用程序,它使用Passport.js在端口3000上进行身份validation.Nginx用作代理服务器来侦听端口80,代理将请求传递到端口3000. Passport.js用于对用户进行身份validation。

身份validation协议如下:用户请求example.com,如果他没有login,则redirect到example.com/login。成功login后,用户将redirect到example.com。

我怀疑它与Nginx有关,以及请求文件的顺序。

Nginxconfiguration:

server { listen 80; location / { proxy_pass http://127.0.0.1:3000; } } 

部分应用程序代码:

 app.post('/api/login', function (req, res, next) { passport.authenticate('local-login', function (err, user, info) { if (err) { return next(err); } if (!user) { return res.status(401).send(info); } req.logIn(user, function (err) { if (err) { return next(err); } return res.send({ username: user.username }); }); })(req, res, next); }); app.get('/', isLoggedIn, function (req, res) { res.sendFile(__dirname + '/client/views/index.html'); }); function isLoggedIn(req, res, next) { if (!req.isAuthenticated()) { res.redirect('/login'); } else { next(); } } 

我想知道是否有人可以帮助我。 我很高兴在需要时提供额外的代码或解释。

Nginx竟然与之无关。 通过直接访问example.com:3000,问题偶尔也会出现。

我注意到浏览器曾经说过我正在访问example.com并显示example.com/login。 我无法在互联网上找到任何证据,但我怀疑Safari / IE9缓存重定向页面并将其链接到原始URL。 所以这里是接下来发生的事情的故事情节。

  1. 用户浏览到example.com/
  2. 获取重定向到example.com/login(浏览器缓存example.com/,但保存登录页面)
  3. 用户登录并被重定向到example.com/
  4. 浏览器有/缓存并加载登录页面。
  5. 开发人员无法解释的错误和令人头痛的问题。

通过添加中间件来解决这个问题。

 app.get('/', isLoggedIn, noCache, function (req, res) { res.sendFile(__dirname + '/client/views/index.html'); }); function noCache(req, res, next) { res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); res.header('Expires', '-1'); res.header('Pragma', 'no-cache'); next(); }