您的位置:首页 > 理论基础 > 计算机网络

Nginx Http认证 实现访问网站或目录密码认证保护 | 使用 HttpAuthBasicModule 模块

2013-08-05 14:26 1046 查看
Nginx Http认证 实现访问网站或目录密码认证保护 |
使用
HttpAuthBasicModule
模块

location  /  {
auth_basic            "Restricted";
auth_basic_user_file  htpasswd;
}


解释:

auth_basic

指令包含一个具有测试用户名和密码的HTTP基本认证,指定的参数将用于认证域。如果将值设置为“off”则忽略下级指令继承的动作。

auth_basic_user_file

指令为验证域指定了密码文件,0.6.7版本以后这里指定的文件是nginx.conf所在目录的相对路径,而不是–prefix指定的路径。

“Restricted" 单词将会出现在第一次访问Nginx站点的弹出框内。

htpasswd是一个文件,位于conf目录下。注意如果你 设置的是 conf/htpasswd,这个htpasswd文件应该在conf/conf/目录下。

或者避免麻烦,直接用绝对路径。

如何生成密码文件(
http://wiki.nginx.org/Faq#How_do_I_generate_an_.htpasswd_file_without_having_Apache_tools_installed.3F


1)使用apache htpasswd 生成:

nginx 的 http auth basic 的密码是用 crypt(3) 加密的,而apache是md5加密。所以生成时:

/usr/local/apache2/bin/htpasswd -c -d pass_file user_name

#回车输入密码,-c 表示生成文件,-d 是以 crypt 加密。


2)使用
htpasswd.py来生成(Nginx 官方 Wiki 中推荐
http://trac.edgewall.org/browser/trunk/contrib/htpasswd.py

chmod 777 htpasswd.py

./htpasswd.py -c -b htpasswd username password


使用反向代理是避免重复提示认证
proxy_set_header Authorization "";

location / {
auth_basic             "Restricted";
auth_basic_user_file   /etc/nginx/htpasswd;
proxy_pass             http://xxx.xxx.xxx.xxx; proxy_redirect         off;
proxy_set_header       Authorization ""; #避免代理weblogic重复提示认证问题
proxy_set_header       Host $host;
proxy_set_header       X-Real-IP $remote_addr;
proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header       X-Forwarded-Ssl on;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: