本文nginx配置参考上一篇文章
利用nginx简单部署一个多域名网站(www,bbs,blog)
要求:访问www.oldboy.com时要输入用户名和密码
首先关闭防火墙。
首先关闭防火墙。
首先关闭防火墙。
systemctl stop firewalld
systemctl disable firewalld
1)修改配置文件
[root@web01 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
auth_basic "安全验证";
auth_basic_user_file /html/www/htpasswd;
}
}
2)创建密码文件
通过htpasswd
命令创建一个有密文信息的密码文件
[root@web01 conf.d]# which htpasswd
/usr/bin/which: no htpasswd in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
检查发现系统没有安装htpasswd软件
yum install -y httpd
[root@web01 conf.d]# rpm -qf `which htpasswd`
httpd-tools-2.4.6-97.el7.centos.5.x86_64
在站点目录下直接创建一个用户名为abc密码是123456的访问密码
[root@web01 www]# htpasswd -bc htpasswd abc 123456
Adding password for user oldboy
[root@web01 www]# ll
total 8
-rw-r--r-- 1 root root 45 Aug 15 20:46 htpasswd
-rw-r--r-- 1 root root 24 Aug 15 19:31 index.html
[root@web01 www]# pwd
/html/www
为了安全起见,只允许nginx用户可以获得该资源
[root@web01 www]# chmod 600 htpasswd
[root@web01 www]# chown nginx.nginx htpasswd
[root@web01 www]# ll
total 8
-rw------- 1 nginx nginx 45 Aug 15 20:46 htpasswd
-rw-r--r-- 1 root root 24 Aug 15 19:31 index.html
[root@web01 conf.d]# ps -ef|grep nginx
root 17911 1 0 21:27 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 17912 17911 0 21:27 ? 00:00:00 nginx: worker process
nginx 17913 17911 0 21:27 ? 00:00:00 nginx: worker process
root 17919 2190 0 21:34 pts/1 00:00:00 grep --color=auto nginx
3)最后的测试结果
[root@web01 conf.d]# curl www.oldboy.com -u abc
Enter host password for user 'abc':
10.0.0.7 www.oldboy.com
在浏览器上,也可以成功登录。