官方链接:https://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
官方案例
该模块可以用来进行基础认证,建立在上一个阶段我们搭建了目录列表。这里就可以通过这个基础认证模块来进行密码的认证
但是他的配置文件是加密的,所以我们需要使用httpd-toos 中的 htpasswd 进行加密
[root@localhost conf.d]# yum -y install httpd-tools[root@localhost conf.d]# htpasswd -c htpasswd kakaNew password:Re-type new password:Adding password for user kaka[root@localhost conf.d]# lshtpasswd Linux-test1.conf zz.conf[root@localhost conf.d]# cat htpasswdkaka:$apr1$S2q9B4Vi$pc6uRsO9yI.K2xEB1yrlR/[root@localhost conf.d]#
那么生成完密码本之后,我们通过配置文件来指定认证
server {listen 8080; #指定监听哪个端口server_name www.test.com; #访问该服务通过哪个域名来访问root /html/game; #存放源码的目录index tndex.html; #定义首页文件location /admin {autoindex on;auth_basic "please input passwd";auth_basic_user_file /etc/nginx/conf.d/htpasswd;}}
再次访问的时候就需要进行认证
