推荐用htpasswd这个命令:
htpasswd -c /etc/nginx/.htpasswd moneyslow
用openssl的命令夜可以,但是笔者遇到不生效的情况,就是随便输入密码也会能访问到,不知道为什么
echo -n 'money:' >> /etc/nginx/.htpasswd
openssl passwd -apr1 >> /etc/nginx/.htpasswd
按提示输入密码
cat /etc/nginx/.htpasswd
Output
money:$apr1$wI1/T0nB$jEKuTJHkTOOWkopnXqC1d1
在nginx配置文件里加auth_basic 的两行:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}