nginx下的用户认证auth_basic ,auth_basic_user_file

当我们不想要网页的某个重要页面被其他用户访问,可以启动认证,需要用户名和密码才能登录访问。

1.首先安装htpasswd,它能为我们给密码加密:

[root@www conf]# yum provides htpasswd
httpd-tools-2.4.6-98.el7.centos.7.x86_64 : Tools for use with the Apache HTTP Server
源    :@updates
匹配来源:
文件名    :/usr/bin/htpasswd
[root@www conf]# yum install httpd-tools  -y

2.生成密码:

[root@sc-nginx conf]# htpasswd -c /usr/local/scnginx/conf/htpasswd long
[root@localhost conf]# cat htpasswd 
long:$apr1$8A/6zU/7$PMJH5eG1oRXkBGpu7RK/x0

-c 指定htpasswd文件存放地址,htpasswd文件存放的路径在conf目录下,不要放在html目录下,因为它属于nginx的一个配置文件,给nginx提供密码的认证的文件,以及指定用户名long

3.在nginx.conf配置文件下的设置,例如状态统计认证:

        location ~ /into {
    
                          #访问位置为/into
                stub_status   on;                        #打开状态统计功能
                access_log off;                          #关闭此位置的日志记录
                auth_basic "scweb";
                auth_basic_user_file htpasswd;
        }

4.测试
访问测试
在这里插入图片描述

若登录错误,在网页右键检查的网络中可发现存在401错误,及无权限,以及我们设置的auth_basic "scweb";
在这里插入图片描述
若登录正确则可以访问到我们用户认证内的信息:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zheng_long_/article/details/130293346