您的位置:首页 > 运维架构 > Nginx

Nginx对某个目录或整个网站进行登录认证的方法

2014-05-31 23:40 274 查看
比如要对 网站目录下的 test 文件夹 进行加密认证



首先需要在opt 的主目录中 /opt/ 创建一个新文件 htpasswd

此文件的书写格式是

用户名:密码

每行一个账户

并且 密码必须使用函数 crypt(3) 加密

官方档说 可以用 Apache 的 htpasswd 工具来创建密码文件

[root@localhost /]# htpasswd

-bash: htpasswd: command not found

[root@localhost /]#

如果上述提示则需要安装httpd

yum install httpd

安装好后执行如下命令

htpasswd -c /opt/nginxpwd user

New password:123456

Re-type new password:123456

Adding password for user ngin

生成用户密钥文件为nginxpwd 用户名为user 密码为123456

密码文件生成好后,在 nginx.conf 文件中对应的 server 段中 添加如下内容

auth_basic "Welcome Back! GUOYU!";

auth_basic_user_file /opt/nginxpwd;

如果想限制某一个目录的话需要如下配置:

location ^~ /test/ {

auth_basic "TEST-Login!";

auth_basic_user_file /opt/nginxpwd;

}

如果 不用 ^~ /test/ 而用 /test 的话 那么将只能对目录进行验证直接访问其下的文件,将不会弹出登录验证

重启Nginx服务,使配置生效
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Nginx nginx登陆