LNMP-Nginx用户验证
需求:有时候我们会有这样一个需求,当网站的某个网页里的页面信息只让公司内部人员才可以访问看到,不想让公司以外人员通过URL访问此网页的页面信息,这时候我们就可以针对网页设置用户验证!
1:使用htpasswd命令生成密码文件
语法:htpasswd 【参数】【存放路径】 【用户】
说明:如果没有这个命令通过YUM安装:yum install -y httpd
[root@ghs vhosts]# htpasswd -c /usr/local/nginx/conf/htpasswd test
New password: ##输入你需要设置的密码
Re-type new password: ##确认密码
Adding password for user test
##显示Adding password for user test这个信息,说明密码文件生成成功,用户为test!查看htpasswd密码文件显示是加密后的文件内容
[root@ghs ~]# cat /usr/local/nginx/conf/htpasswd
test:$apr1$tFA8lqrc$DSJA5/fBmybZmJxGHe4l0.
2:编辑配置文件
[root@ghs ~]# vim /usr/local/nginx/conf/vhosts/test.conf
添加下面这段配置信息
location / {
auth_basic " Please enter your username for access";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
说明:auth_basic定义的是访问时输出的页面显示的信息,内容可自定义!auth_basic_user_file后面定义的是存放的密码文件路径
3:保存之后重启Nginx
[root@ghs ~]# nginx -s reload
三:验证
在浏览访问URL显示下面图片,表示设置用户验证成功
记录每一天有趣的事情!!

浙公网安备 33010602011771号