1. nginx 配置文件增加 basic 配置
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /home/web;
auth_basic "for nginx auth";
auth_basic_user_file conf/server.auth; # 存储用户和密码的文件
# auth_basic_user_file /home/web/authconf/server.auth; # 也可以使用绝对路径,但是这个路径,nginx 必须要有可读权限
autoindex on;
location / {
}
}
- auth_basic string | off; # string 会在弹窗中显示 - http, server, location, limit_except
- auth_basic_user_file auth_file; - http, server, location, limit_except
2. 密码文件的格式
a-username:hash-password
3. 使用 openssl 命令创建密码
貌似 ubuntu 中的 openssl passwd
不支持 -crypt
参数了,可以把 -crypt
换成 -1
()数字1
opensl passwd -1 a-simple-passwd
旧版本的 openssl 可以使用下面的命令
$ openssl passwd -crypt a-simple-password # 假设密码是 a-simple-password
Warning: truncating password to 8 characters
K5tJruB33QlDo # 加密后的密码就是 K5tJruB33QlDo,在 user file 中填写的就是用户名和这个加密后的密码