Nginx伺服器中為網站或目錄添加認證密碼的配置詳解_nginx

來源:互聯網
上載者:User

nginx可以為網站或目錄甚至特定的檔案設定密碼認證。密碼必須是crypt加密的。可以用apache的htpasswd來建立密碼。

格式為:

htpasswd -b -c site_pass username password

site_pass為密碼檔案。放在同nginx設定檔同一目錄下,當然你也可以放在其它目錄下,那在nginx的設定檔中就要寫明絕對位址或相對目前的目錄的地址。

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

yum install httpd

如果你不想安裝httpd的話,可以使用perl指令碼來實現(代碼如下:)

#! /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;  } 
為指令碼賦予可執行許可權:
chmod o+x add_user.pl

指令碼使用方法:

./add_user.pluser:password

把產生的使用者名稱密碼粘貼到/usr/local/nginx/conf/vhost/nginx_passwd檔案中即可

如果是為了給網站加上認證,可以直接將認證語句寫在nginx的配置server段中。

如果是為了給目錄加上認證,就需要寫成目錄形式了。同時,還要在目錄中加上php的執行,否則php就會被下載而不執行了。

例如:基於整個網站的認證,auth_basic在php解釋之前。

server  {    listen 80;    server_name www.jb51.net 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;  } 

針對目錄的認證,在一個單獨的location中,並且在該location中嵌套一個解釋php的location,否則php檔案不會執行並且會被下載。auth_basic在嵌套的location之後。

server  {    listen 80;    server_name www.jb51.net 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;  } 

這裡有一個細節,就是location ~ ^/admin/.* {…} 保護admin目錄下的所有檔案。如果你只設了/admin/ 那麼直接輸入/admin/index.php還是可以訪問並且啟動並執行。 ^/admin/.* 意為保護該目錄下所有檔案。當然,只需要一次認證。並不會每次請求或每請求一個檔案都要認證一下。

htpasswd

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

-c 建立passwdfile.如果passwdfile 已經存在,那麼它會重新寫入並刪去原有內容.
-n 不更新passwordfile,直接顯示密碼
-m 使用MD5加密(預設)
-d 使用CRYPT加密(預設)
-p 使用普通文字格式設定的密碼
-s 使用SHA加密
-b 命令列中一併輸入使用者名稱和密碼而不是根據提示輸入密碼,可以看見明文,不需要互動
-D 刪除指定的使用者
執行個體
1. 如何利用htpasswd命令添加使用者?

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

產生目前的目錄下產生一個linuxeye_pd檔案,使用者名稱linuxeye_user,密碼:linuxeye_password,預設採用MD5加密方式

2. 如何在原有密碼檔案中增加下一個使用者?

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

一定要去掉-c選項,否則覆蓋密碼檔案再建立

3. 如何不更新密碼檔案,只顯示加密後的使用者名稱和密碼?

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

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

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

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.