Nginx 用户认证

访问一些比较私密的页面 ( 如管理后台,数据后台 ) 时,我们就可以设置访问该页面时需要使用用户名和密码进行验证,这就是用户认证

[root@localhost ~]$ cat /usr/local/nginx/conf/vhost/test.com.conf 
server {
    listen 80;
    server_name www.test.com;
    index index.html index.html index.php;
    root /data/www;

    location /admin/ {
        auth_basic            "Auth";                            # 指定认证说明信息
        auth_basic_user_file  /usr/local/nginx/conf/htpasswd;    # 指定用户认证文件
    }
}
[root@localhost ~]$ yum install -y httpd-tools                          # 需要使用到 Apache 的 htpasswd 工具来创建认证用户
[root@localhost ~]$ htpasswd -c /usr/local/nginx/conf/htpasswd user1    # 创建第一个认证用户要用 -c Create a new file 参数
[root@localhost ~]$ htpasswd /usr/local/nginx/conf/htpasswd user2       # 创建第二个认证用户不需要再加 -c 参数
[root@localhost ~]$ /usr/local/nginx/sbin/nginx -t   
[root@localhost ~]$ /usr/local/nginx/sbin/nginx -s reload

 

 

 

 

 

 

 

 

 

    

posted @ 2019-01-29 10:17  孔雀东南飞  阅读(245)  评论(0编辑  收藏  举报