欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nginx服務(wù)器中為網(wǎng)站或目錄添加認(rèn)證密碼的配置詳解

 更新時(shí)間:2016年01月07日 09:25:19   作者:moon  
這篇文章主要介紹了Nginx服務(wù)器中為網(wǎng)站或目錄添加認(rèn)證密碼的配置詳解,使用到了Apache的htpasswd工具,需要的朋友可以參考下

nginx可以為網(wǎng)站或目錄甚至特定的文件設(shè)置密碼認(rèn)證。密碼必須是crypt加密的??梢杂胊pache的htpasswd來創(chuàng)建密碼。

格式為:

htpasswd -b -c site_pass username password

site_pass為密碼文件。放在同nginx配置文件同一目錄下,當(dāng)然你也可以放在其它目錄下,那在nginx的配置文件中就要寫明絕對地址或相對當(dāng)前目錄的地址。

如果你輸入htpasswd命令提示沒有找到命令時(shí),你需要安裝httpd。如果是centos可以執(zhí)行如下來安裝,

yum install httpd

如果你不想安裝httpd的話,可以使用perl腳本來實(shí)現(xiàn)(代碼如下:)

#! /usr/bin/perl -w  
#filename: add_ftp_user.pl  
use strict;  
#  
print "#example: user:passwd\n";  
while (<STDIN>) {  
  exit if ($_ =~/^\n/);  
  chomp;  
  (my $user, my $pass) = split /:/, $_, 2;  
  my $crypt = crypt $pass, '$1$' . gensalt(8);  
  print "$user:$crypt\n";  
}  
sub gensalt {  
  my $count = shift;  
  my @salt = ('.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z');  
  my $s;  
  $s .= $salt[rand @salt] for (1 .. $count);  
  return $s;  
} 

為腳本賦予可執(zhí)行權(quán)限:
chmod o+x add_user.pl

腳本使用方法:

./add_user.pl
user:password

把生成的用戶名密碼粘貼到/usr/local/nginx/conf/vhost/nginx_passwd文件中即可

如果是為了給網(wǎng)站加上認(rèn)證,可以直接將認(rèn)證語句寫在nginx的配置server段中。

如果是為了給目錄加上認(rèn)證,就需要寫成目錄形式了。同時(shí),還要在目錄中加上php的執(zhí)行,否則php就會(huì)被下載而不執(zhí)行了。

例如:基于整個(gè)網(wǎng)站的認(rèn)證,auth_basic在php解釋之前。

server  
{  
  listen 80;  
  server_name www.dbjr.com.cn jb51.net;  
  root /www/jb51.net;  
  index index.html index.htm index.php;  
  auth_basic "input you user name and password";  
  auth_basic_user_file /usr/local/nginx/conf/vhost/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/jb51.net_access.log main;  
} 

針對目錄的認(rèn)證,在一個(gè)單獨(dú)的location中,并且在該location中嵌套一個(gè)解釋php的location,否則php文件不會(huì)執(zhí)行并且會(huì)被下載。auth_basic在嵌套的location之后。

server  
{  
  listen 80;  
  server_name www.dbjr.com.cn jb51.net;  
  root /www/jb51.net;  
  index index.html index.htm index.php;  
  location ~ ^/admin/.*  
  {  
  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/conf/vhost/auth/admin.pass;  
  }  
  location ~ .php$  
  {  
    fastcgi_pass 127.0.0.1:9000;  
    fastcgi_index index.php;  
    include fastcgi_params;  
  }  
  location ~ /\.ht  
  {  
    deny all;  
  }  
  access_log /logs/jb51.net_access.log main;  
} 

這里有一個(gè)細(xì)節(jié),就是location ~ ^/admin/.* {…} 保護(hù)admin目錄下的所有文件。如果你只設(shè)了/admin/ 那么直接輸入/admin/index.php還是可以訪問并且運(yùn)行的。 ^/admin/.* 意為保護(hù)該目錄下所有文件。當(dāng)然,只需要一次認(rèn)證。并不會(huì)每次請求或每請求一個(gè)文件都要認(rèn)證一下。

htpasswd

既然是使用到了htpasswd,那么就順帶著介紹一下其基本使用。
htpasswd參數(shù)

-c 創(chuàng)建passwdfile.如果passwdfile 已經(jīng)存在,那么它會(huì)重新寫入并刪去原有內(nèi)容.
-n 不更新passwordfile,直接顯示密碼
-m 使用MD5加密(默認(rèn))
-d 使用CRYPT加密(默認(rèn))
-p 使用普通文本格式的密碼
-s 使用SHA加密
-b 命令行中一并輸入用戶名和密碼而不是根據(jù)提示輸入密碼,可以看見明文,不需要交互
-D 刪除指定的用戶
實(shí)例
1. 如何利用htpasswd命令添加用戶?

# /usr/local/apache/bin/htpasswd -bc linuxeye_pd linuxeye_user linuxeye_password
Adding password for user linuxeye_user
# cat linuxeye_pd
linuxeye_user:$apr1$Mugpp3FE$zGsi7/JfQIhFXPlgqo/Wx/

生成當(dāng)前目錄下生成一個(gè)linuxeye_pd文件,用戶名linuxeye_user,密碼:linuxeye_password,默認(rèn)采用MD5加密方式

2. 如何在原有密碼文件中增加下一個(gè)用戶?

# /usr/local/apache/bin/htpasswd -b linuxeye_pd linuxeye.com linuxeye.com
Adding password for user linuxeye.com
# cat linuxeye_pd
linuxeye_user:$apr1$Mugpp3FE$zGsi7/JfQIhFXPlgqo/Wx/
linuxeye.com:$apr1$/8EUOPYI$4MBxYpzotrSDcTTDZvTeT0

一定要去掉-c選項(xiàng),否則覆蓋密碼文件再創(chuàng)建

3. 如何不更新密碼文件,只顯示加密后的用戶名和密碼?

# /usr/local/apache/bin/htpasswd -n linuxeye
New password:
Re-type new password:
linuxeye:$apr1$bZ6Gclc4$zKRap.0BADzZIxLoxpDNv0
 
# /usr/local/apache/bin/htpasswd -nb linuxeye linuxeye_password
linuxeye:$apr1$yvngdKGV$QrnlriJ.MxIu52Vmo.ROE1

4. 如何利用htpasswd命令刪除用戶名和密碼?

# /usr/local/apache/bin/htpasswd -D linuxeye_pd linuxeye_user
Deleting password for user linuxeye_user
# cat linuxeye_pd
linuxeye.com:$apr1$/8EUOPYI$4MBxYpzotrSDcTTDZvTeT0

5. 如何利用htpasswd命令修改密碼?

# /usr/local/apache/bin/htpasswd -D linuxeye_pd linuxeye.com
Deleting password for user linuxeye.com
# /usr/local/apache/bin/htpasswd -b linuxeye_pd linuxeye.com linuxeye_passwd
Adding password for user linuxeye.com
# cat linuxeye_pd
linuxeye.com:$apr1$74ZvB1vC$/b7ETmg8xhDPieYj0b0cE.

相關(guān)文章

最新評論