首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Nginx上配置Http Basic Auth保护目录

2013-01-28 
Nginx下配置Http Basic Auth保护目录Nginx下配置Http Basic Auth保护目录iframe idaswift_0 stylema

Nginx下配置Http Basic Auth保护目录

Nginx下配置Http Basic Auth保护目录

<iframe id="aswift_0" style="margin: 0px; padding: 0px; border-width: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; left: 0px; position: absolute; top: 0px;" name="aswift_0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="468" height="60"></iframe>

一直在Apache下使用HTTP basic auth(.htpasswd)来保护网站的某些目录的访问,现在VPS上换成了Nginx同样需要保护一下。

Nginx下的配置也挺方便的,我们可以沿用由Apache的htpasswd模块生成的.htpasswd文件作为密码文件。注意,nginx 的 http auth basic 的密码是用 crypt(3) 加密的,而apache是md5加密。所以生成时:

1/usr/local/apache2/bin/htpasswd?-c?-d?pass_file?user_name2#回车输入密码,-c?表示生成文件,-d?是以?crypt?加密。

对于lnmp用户,一般不安装apache了,文末老N会告诉你个生成方法。

我们将这个htpasswd文件放到nginx/conf下,记得chmod 400 htpasswd来保护一下。

然后修改nginx.conf:

?

01server {02????server_name d8.neolee.com;03????root /var/www/d8.neolee.com;04????include?/etc/nginx/fastcgi_php;05????location / {06?????auth_basic????????????"Password please";07?????auth_basic_user_file? /usr/local/nginx/conf/htpasswd;08????????index index.php;09????????if?(!-e?$request_filename) {10????????????rewrite ^(.*)$? /index.php last;11????????}12????}13}

加入了

1auth_basic????????????"Password please";2auth_basic_user_file? /usr/local/nginx/conf/htpasswd;

重启nginx即可。
给了例子看看,请访问http://d8.neolee.com 用户名neo 密码123

?

========

最后我们说下怎么在nginx下生成htpasswd

下载这个python文件:http://trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py?(nginx wiki里推荐的)

运行示例

1chmod?777 htpasswd.py2./htpasswd.py -c -b htpasswd username password3#-c为生成文件 htpasswd为文件名

热点排行