Nginx对某个目录设置密码保护例子
有时不想某个目录被访问,所以要加密码之类来保护,以前用过的 Apache 好像很简单就实现目录加密。我用的是 Nginx,用了那么久 Nginx 还真没试过加密……
参考了一些资料,然后发现这样就可以:(Debian系统,配置中“#.......”表示后续还有其他配置)
1. 配置网站(假如 yousite.com 放在 /home/www,然后要对 ooxx 目录加密)
代码如下:
server {
listen 80;
server_name yousite.com;
root /home/www;
index index.php index.html;
location ^~ /ooxx/{
auth_basic "Authorized users only";
auth_basic_user_file /home/.htpasswd;
}
listen 80;
server_name yousite.com;
root /home/www;
index index.php index.html;
location ^~ /ooxx/{
auth_basic "Authorized users only";
auth_basic_user_file /home/.htpasswd;
}
#......
}