Nginx加密认证目录或者网站

Tuesday, April 12, 2011

在apahe下面可以对网站或者目录/文件做密码认证, 当然nginx也能做。 在nginx中, 密码必须是crypt加密的,
可以使用apache的htpasswd来生成密码 (没有就装一个mini-httpd)。 命令: htpasswd -b -c passwordfile username password 例:htpasswd -b -c lxneng.com.pass lxneng 123456

1, 基于整个网站的配置

server {
        listen       80;
        server_name  dl.lxneng.com;
        root /var/www/lxneng.com/downloads;
        auth_basic "Please input your username and password!";
        auth_basic_user_file /etc/nginx/auth/lxneng.com.pass;
       }

2, 基于目录的配置

server {
        listen       80;
        server_name  lxneng.com *.lxneng.com;
        root /var/www/lxneng.com;
        location ~ ^/downloads/.* {
                auth_basic "Please input your username and password!";
                auth_basic_user_file /etc/nginx/auth/lxneng.com.pass;
                }
       }

这里 location \~ \^/downloads/.* 保护该目录下的所有文件,只要认证一次就可以访问该目录下的所有文件。 效果:
image

This entry was tagged Linux and Nginx

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top