简体   繁体   English

Nginx + Express仅向经过身份验证的用户提供静态文件

[英]nginx + express serve static files only to authenticated users

I'm working on a static website built with gatsby. 我正在使用gatsby构建的静态网站。

I want it to be accessible only for visitors who know the password and I can't do it with the .htaccess since i want my own custom "login" page, not just some brower's popup. 我希望只有知道密码的访问者才能访问它,而我无法使用.htaccess进行访问,因为我希望拥有自己的自定义“登录”页面,而不仅仅是浏览器的弹出窗口。

I figured that i would just build a very simple express API with the following endpoints: 我认为我将使用以下端点构建一个非常简单的express 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

Everything works fine, but Im a bit concerned about not using nginx. 一切正常,但是我有点担心不使用nginx。 I will be deploying the app soon (digital ocean) and im trying to understand how the performance of serving the static files via express instead of nginx lowers the speed. 我将很快部署该应用程序(数字海洋),我将尝试了解通过express而不是nginx服务静态文件的性能如何降低速度。

My question is, how can I implement it in the way that its nginx that serves the files, but if there is no cookie then redirects to "/login" just like now? 我的问题是,我该如何以为其文件提供服务的nginx的方式来实现它,但是如果没有cookie,则像现在一样重定向到“ / login”?

I keep reading a lot and i have no problem doing the serving via nginx part, but i have problems understanding "check-if-cookie" flow. 我经常阅读很多书,通过nginx部分进行投放没有问题,但是我在理解“ check-if-cookie”流程时遇到了问题。

Is it even a good practice to check cookie (and redirect if not found) in nginx? 在nginx中检查cookie(如果找不到则重定向)甚至是一个好习惯? Or maybe i should do things differently? 也许我应该做些不同的事情?

Thanks for any help. 谢谢你的帮助。

function checkAuth(req, res, next) {
    if (!checkAuth) {
        res.redirect('/login');
    } else {
        next();
    }
}

app.use('/admin/information', checkAuth, information);

Do same concept as above. 执行与上述相同的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM