nginx设置用户访问认证功能


本文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

在浏览器上,也可以成功登录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值