我有一个仅供 3 位程序员私有(private)使用的网站。它是由 nginx 直接提供的简单 HTML,但旨在供办公室内外使用。
我想要一个简单的密码或身份验证方案。我可以使用 HTTP 身份验证,但这些往往会经常过期,这让人们使用起来很痛苦。我也很紧张,有人闻比 cookies 更容易闻。
所以我想知道我是否可以在他们的浏览器上使用唯一的长 ID 在 JavaScript 中设置一个 cookie,并以某种方式告诉 nginx 只接受具有此 cookie 的请求(针对特定子域)。
这足够简单吗?我如何
最佳答案
我从 a blog post by Christian Stocker 找到了一个看起来非常简单的解决方案.它执行以下规则:
这真是两全其美。
这是配置:
map $cookie_letmein $mysite_hascookie {
"someRandomValue" "yes";
default "no";
}
geo $mysite_geo {
192.168.0.0/24 "yes"; #some network which should have access
10.10.10.0/24 "yes"; #some other network which should have access
default "no";
}
map $mysite_hascookie$mysite_geo $mysite_authentication{
"yesyes" "off"; #both cookie and IP are correct => OK
"yesno" "off"; #cookie is ok, but IP not => OK
"noyes" "off"; #cookie is not ok, but IP is ok => OK
default "Your credentials please"; #everythingles => NOT OK
}
server {
listen 80;
server_name mysite.example.org;
location / {
auth_basic $mysite_authentication;
auth_basic_user_file htpasswd/mysite;
add_header Set-Cookie "letmein=someRandomValue;max-age=3153600000;path=/"; #set that special cookie, when everything is ok
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
关于authentication - 在 nginx 上使用一次性 cookie 进行非常简单的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10718895/