如何使用Nginx+htpasswd保护隐私目录?

技术运维Nginx 374

Nginx创建敏感目录的认证访问方式。

》一文中介绍了Apache的实现方式以及htpasswd命令的具体用法,如果你对htpasswd还不了解先去翻一下前文。

同样,先用htpasswd命令创建你授权用户和密码:

[root@iZ2zegabytb6377yktqv10Z xxx]# htpasswd -c /path/to/file/.htpasswd yourusername
New password: 
Re-type new password: 
Adding password for user yourusername

配置Nginx .conf文件,找到server{},添加:

    location /path/to/folder{
        auth_basic "Administrator Login";
        auth_basic_user_file /path/to/file/.htpasswd;
    }

别忘了测试和reload Nginx

/usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
/usr/sbin/nginx -s reload

大功告成!

Post Comment