环境:centos 7.6,nginx1.16
首先要安装httpd-tools
[root@nginx ~]# yum install -y httpd-tools
1、先创建用户认证文件:
[root@nginx ~]# htpasswd -c /data/www/blog/passwd test1
New password:
Re-type new password:
Adding password for user test1
[root@nginx ~]# cat /data/www/blog/passwd
test1:$apr1$52kM49ex$1NWEsuliHX2Ro4yXpzDOI/
[root@nginx ~]#
2、配置nginx
以tmp目录为例,当用户访问tmp目录时需要输入密码:
location /tmp {
auth_basic "请输入用户名密码";
auth_basic_user_file /data/www/blog/passwd;
}
重新加载nginx来测试一下: