CenOS7.6 nginx用户认证

[root@localhost conf.d]# nginx -v
nginx version: nginx/1.16.1
[root@localhost conf.d]# hostnamectl
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: 8502af7175ae42b3bf840df5de6a746c
Boot ID: 7976a1d88f9a459c83fd42550d66606f
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-957.el7.x86_64
Architecture: x86-64

1.yum -y install httpd-tools # 安装 htpasswd 工具

[root@localhost conf.d]# cd /etc/nginx/conf.d/

2.htpasswd -c pass.db ybj # 创建passwordfile并添加认证用户 ybj

会在/etc/nginx/conf.d/passwd.db文件中生成用户名和加密的密码:
ybj: a p r 1 apr1 apr1Xq0d6gYO$g4Txsf.xkJjEsyTw8pJ22.

在原先的passwordfile基础上在新增账号
htpasswd pass.db jby

passwordfile上删除指定账号
htpasswd -D pass.db ybj

3.nginx增加auth_basic和auth_basic_user_file两项配置:

[root@localhost conf.d]# vi /etc/nginx/conf.d/default.conf
#auth_basic代表登陆提示信息
#auth_basic_user_file代表调用的passwordfile

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    auth_basic "ybj 's nginx";      
    auth_basic_user_file        /etc/nginx/conf.d/pass.db;   

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80

“/etc/nginx/conf.d/default.conf” 48L, 1177C

4 重启nginx服务

 systemctl restart nginx

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值