nginx为目录或网站加上密码认证 [转]

nginx可以为网站或目录甚至特定的文件设置密码认证。密码必须是crypt加密的。可以用apache的htpasswd来创建密码。

格式为:

  1. htpasswd -b -c site_pass username password

site_pass为密码文件。放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址。

如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd。如果是centos可以执行如下来安装,

  1. yum install httpd

如果你不想安装httpd的话,可以使用perl脚本来实现(代码如下:)

  1. #! /usr/bin/perl -w   
  2. #filename: add_ftp_user.pl   
  3. use strict;   
  4. #   
  5. print "#example: user:passwd\n";   
  6. while (<STDIN>) {   
  7.     exit if ($_ =~/^\n/);   
  8.     chomp;   
  9.     (my $usermy $pass) = split /:/, $_, 2;   
  10.     my $crypt = crypt $pass, '$1$' . gensalt(8);   
  11.     print "$user:$crypt\n";   
  12. }   
  13. sub gensalt {   
  14.     my $count = shift;   
  15.     my @salt = ('.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z');   
  16.     my $s;   
  17.     $s .= $salt[rand @saltfor (1 .. $count);   
  18.     return $s;   
  19. }  

为脚本赋予可执行权限:

  1. chmod o+x add_user.pl

脚本使用方法:

  1. ./add_user.pl
  2. user:password

把生成的用户名密码粘贴到/usr/local/nginx/conf/vhost/nginx_passwd文件中即可

如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。

如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。

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

  1. server   
  2. {   
  3.     listen 80;   
  4.     server_name www.92csz.com 92csz.com;   
  5.     root /www/92csz.com;   
  6.     index index.html index.htm index.php;   
  7.     auth_basic "input you user name and password";   
  8.     auth_basic_user_file /usr/local/nginx/conf/vhost/nginx_passwd;   
  9.     location ~ .php$   
  10.     {   
  11.         fastcgi_pass 127.0.0.1:9000;   
  12.         fastcgi_index index.php;   
  13.         include fastcgi_params;   
  14.     }   
  15.     location ~ /\.ht   
  16.     {   
  17.         deny all;   
  18.     }   
  19.     access_log /logs/92csz.com_access.log main;   
  20. }  

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

  1. server   
  2. {   
  3.     listen 80;   
  4.     server_name www.92csz.com 92csz.com;   
  5.     root /www/92csz.com;   
  6.     index index.html index.htm index.php;   
  7.     location ~ ^/admin/.*   
  8.     {   
  9.     location ~ \.php$   
  10.     {   
  11.         fastcgi_pass 127.0.0.1:9000;   
  12.         fastcgi_index index.php;   
  13.         include fastcgi_params;   
  14.     }   
  15.     auth_basic "auth";   
  16.     auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass;   
  17.     }   
  18.     location ~ .php$   
  19.     {   
  20.         fastcgi_pass 127.0.0.1:9000;   
  21.         fastcgi_index index.php;   
  22.         include fastcgi_params;   
  23.     }   
  24.     location ~ /\.ht   
  25.     {   
  26.         deny all;   
  27.     }   
  28.     access_log /logs/92csz.com_access.log main;   
  29. }  

这里有一个细节,就是location ~ ^/admin/.* {…} 保护admin目录下的所有文件。如果你只设了/admin/ 那么直接输入/admin/index.php还是可以访问并且运行的。 ^/admin/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值