龙岩易富通网络科技有限公司

龙岩小程序开发,龙岩分销系统

nginx配置Http Basic Auth保护目录

2015.09.10 | 1312阅读 | 0条评论 | 通用代码

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

htpasswd -b -c site_pass username password

例如:基于整个网站的认证,auth_basic在php解释之前。 

server { 

   listen 80; 

   server_name www.0597seo.com 0597seo.com; 

   root /wwwroot/0597seo.com; 

   index index.html index.htm index.php; 

   auth_basic "auth"; 

   auth_basic_user_file /usr/local/nginx/auth/nginx_passwd; 

   location ~ .php$ { 

      fastcgi_pass 127.0.0.1:9000; 

     fastcgi_index index.php; 

     include fastcgi_params; 

}

location ~ /\.ht { 

  deny all; 

}

access_log /logs/0597seo.com_access.log main; 

}

针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。

auth_basic在嵌套的location之后。 

server { 

   listen 80; 

   server_name www.0597seo.com 0597seo.com; 

   root /wwwroot/0597seo.com; 

   index index.html index.htm index.php; 

   location ~ ^/phpMyAdmin/.* { 

      location ~ \.php$ { 

          fastcgi_pass 127.0.0.1:9000; 

          fastcgi_index index.php; 

          include fastcgi_params; 

    }

 auth_basic "auth"; 

 auth_basic_user_file /usr/local/nginx/auth/auth_phpMyAdmin; 

}

location ~ .php$ { 

  fastcgi_pass 127.0.0.1:9000; 

  fastcgi_index index.php; 

  include fastcgi_params; 

}

location ~ /\.ht { 

  deny all; 

}

access_log /logs/0597seo.com_access.log main; 

}

这里注意,就是location ~ ^/phpMyAdmin/.* {…} 保护phpMyAdmin目录下的所有文件。

如果你只设了/phpMyAdmin/ 那么直接输入/phpMyAdmin/index.php还是可以访问并且运行的。 

^/phpMyAdmin/.* 意为保护该目录下所有文件。



htpasswd.zip


赞 (

发表评论