首页 文章

nginx express仅向经过身份验证的用户提供静态文件

提问于
浏览
0

我正在使用gatsby构建的静态网站 .

我希望只有知道密码的访问者才能访问它,而我无法使用.htaccess进行访问,因为我想要自己的自定义“登录”页面,而不仅仅是浏览器的弹出窗口 .

我想我会用以下 endpoints 构建一个非常简单的快速API:

"/" - serves the static files (gatsby build files, the actual website), 
if you dont have a certain cookie, you get redirected to "/login"

GET "/login" - my custom login page (simple input for the password)

POST "/login" - in here you send the password, if its correct, 
you get the cookie and you get redirected to "/" so to the actual website

一切正常,但我有点担心不使用nginx . 我将很快部署应用程序(数字海洋),我试图了解如何通过express而不是nginx提供静态文件的性能降低了速度 .

我的问题是,如何以提供文件的nginx的方式实现它,但如果没有cookie,那么就像现在一样重定向到“/ login”?

我继续阅读很多,我通过nginx部分做服务没有问题,但我在理解“check-if-cookie”流程时遇到了问题 .

在nginx中检查cookie(如果没有找到重定向)甚至是一个好习惯?或者我应该以不同的方式做事?

谢谢你的帮助 .

1 回答

  • 0
    function checkAuth(req, res, next) {
        if (!checkAuth) {
            res.redirect('/login');
        } else {
            next();
        }
    }
    
    app.use('/admin/information', checkAuth, information);
    

    做与上面相同的概念 .

相关问题