1.官网
https://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
2.语法
3.创建密码
[root@localhost ~]# yum install httpd-tools -y
4.创建密码文件
完毕!
[root@localhost ~]# htpasswd -b -c /etc/nginx/auth-passwd xp xp666
-c 创建passwdfile ,如果passwdfile文件已经存在,那么他就会重新写入,并删除原有的内容
-b 命令行中一并输入用户和密码,而不是根据提示去输入用户密码 可以看见是明文,并不需要去交互
5.往配置文件中写入两行
server {
listen 80;
server_name 192.168.134.139;
location / {
auth_basic "please input your account password";
auth_basic_user_file /etc/nginx/auth_passwd;
root /www/xp;
index index.html;
}
}
auto_bassic 是给用户看的提示
auto_basic_user_file 你的密码文件
6.重启nginx
[root@localhost ~]# systemctl restart nginx
7.再次访问