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:apr1apr1Xq0d6gYO$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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章