类别:技术积累 / 日期:2021-09-30 / 浏览:2916 / 评论:0
假设一个网站没有实现登录认证功能,我们要临时增加一个简单的账号密码认证,可以使用Nginx来实现该功能。
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://test_server/;
auth_basic "Please Input Password: ";# 账号密码认证提示信息
auth_basic_user_file conf/htpasswd;# 密码文件
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}auth_basic_user_file conf/htpasswd指示存储密码的文件位置,该文件格式如下:
# comment account1:password1 account2:password2:comment
密码应该使用crypt()函数加密(在线加密:https://tool.oschina.net/htpasswd),也可以用Apache HTTP Server发行包中的htpasswd命令或者openssl passwd来创建此类文件。
发表评论 / 取消回复