官方指导文档:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

应用场景:企业在实际工作要求我们为网站设置访问账号和密码权限,这样操作后,只有拥有账号密码的用户才可以访问网站内容。

location / {
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}


关键词解析:
auth_basic
Synatx: auth_basic string|off;
# 输入密码之前的提示,默认关,
Default::auth_basic off
放置标签:http, server, location, limit_except
auth_basic_user_file file
Synatx: auth_basic_user_file file;
# 指向存放用户名密码的文件
Default: —
Context: http, server, location, limit_except
Specifies a file that keeps user names and passwords, in the following format:
# comment
name1:password1 name2:password2:comment
name3:password3

 

实战演练

在server虚拟主机配置

Nginx  用户访问及密码验证_apache

设置密码文本【本次仅做演示,所以操作用文本操作】

Nginx  用户访问及密码验证_用户名_02

   补充:httpasswd为apache自带包,安装apache之后就安装了httpasswd包

             此处 -c 为指定文件  -b为非交互输入  

效果展示

Nginx  用户访问及密码验证_d3_03