Nginx使用htpasswd创建用户认证
hebiwtc 2011-02-27
Linux下执行,生成认证文件/usr/local/nginx/conf/htpasswd/cacti
/usr/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/cacti addcn
配置nginx文件,/usr/local/nginx/conf/nginx.conf
# cacti
location /cacti/ {
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/htpasswd/cacti;
}
值得注意的是 ,如果对 /private/目录做了限制,那么/private/filename.php 是不生效的,因为后面的正则表达式又对php进行了fastcgi转发。
解决办法就是:单独把private目录下的php文件限制也写到规则里面,而且在php文件解析的规则之前。
#deny private
location /private/ {
allow 222.222.222.35;
allow 192.168.1.0/249;
deny all;
}
#deny private/filename.php
location ~ ^/private/.*\.php$
{
allow 222.222.222.35;
allow 192.168.1.0/249;
deny all;
}
#php
location ~ .*\.php?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}