1.创建htpasswd文件:
可以使用以下这个python脚本生成:
http://trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py
执行命令:
1 2 | chmod 777 htpasswd.py ./htpasswd.py -c -b htpasswd username password |
其中htpasswd是生成的文件名
2.修改nginx的conf
修改nginx.conf或者所要设置的vhost的conf,加入如下语句:
1 2 3 4 5 | location ^~ / { auth_basic "Password"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } |
其中htpasswd是上面步骤一生成的文件,我放到了nginx的conf文件夹下。
这样就对整个根目录进行了加密,但是出现了一个问题就是无法解析php。
那么将上述文件中的php解析部分改成:
1 2 3 4 5 6 7 8 9 | location ^~ / { location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } auth_basic "Password"; uth_basic_user_file /usr/local/nginx/conf/htpasswd; } |
即可。
近期评论