簡體   English   中英

Nginx + Express僅向經過身份驗證的用戶提供靜態文件

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

我正在使用gatsby構建的靜態網站。

我希望只有知道密碼的訪問者才能訪問它,而我無法使用.htaccess進行訪問,因為我希望擁有自己的自定義“登錄”頁面,而不僅僅是瀏覽器的彈出窗口。

我認為我將使用以下端點構建一個非常簡單的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

一切正常,但是我有點擔心不使用nginx。 我將很快部署該應用程序(數字海洋),我將嘗試了解通過express而不是nginx服務靜態文件的性能如何降低速度。

我的問題是,我該如何以為其文件提供服務的nginx的方式來實現它,但是如果沒有cookie,則像現在一樣重定向到“ / login”?

我經常閱讀很多書,通過nginx部分進行投放沒有問題,但是我在理解“ check-if-cookie”流程時遇到了問題。

在nginx中檢查cookie(如果找不到則重定向)甚至是一個好習慣? 也許我應該做些不同的事情?

謝謝你的幫助。

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

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

執行與上述相同的概念。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM