为访问nginx目录添加权限认证auth_basic

zz from:http://zhys9.com/blog/?p=294

 

由于nginx的auth_basic认证采用的是与apache兼容密码文件,因此需要用到apache的htpasswd用以生成密码文件。

首先且到root, 安装httpd (如果已经安装的,可以跳过)

/usr/bin/yum install httpd

安装完毕之后就可以先找一下htpasswd的位置

/usr/bin/whereis htpasswd
htpasswd: /usr/bin/htpasswd /usr/share/man/man1/htpasswd.1.gz

可以看到路径是 /usr/bin/htpasswd

接下来就生成密码文件并添加一个用户(master)吧

/usr/bin/htpasswd -c /home/nginx/conf/authdb master

会得到以下提示信息
New password:
输入密码之后,系统会要求再次输入密码。确认之后即添加完毕。

然后就是修改nginx的配置文件,在需要使用auth_basic添加配置;
例如需要在 http://zhys9.com/secret/ 目录使用;

/bin/vi /home/nginx/conf/nginx.conf

找到zhys9.com对应的server段

增加一个location段

location /secret/
{
    auth_basic  "Secret Garden.";
    auth_basic_user_file  /home/nginx/conf/authdb;
}

保存退出之后,要记得reload一下nginx

/home/nginx/sbin/nginx -s reload

ok, 访问http://zhys9.com/secret/ 看看是否生效了?

猜你喜欢

转载自laibulai.iteye.com/blog/1005986