Nginx服務(wù)器中為網(wǎng)站或目錄添加認(rèn)證密碼的配置詳解
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; }
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)文章
瀏覽器控制臺(tái)報(bào)錯(cuò)Failed to load module script:解決方
這篇文章主要為大家介紹了瀏覽器控制臺(tái)報(bào)錯(cuò)Failed to load module script:解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Nginx服務(wù)器中l(wèi)ocation配置的一些基本要點(diǎn)解析
這篇文章主要介紹了Nginx服務(wù)器中l(wèi)ocation配置的一些基本要點(diǎn)解析,特別對管理以及查找匹配作出了詳細(xì)的講解,需要的朋友可以參考下2015-12-12nginx 502、413和404錯(cuò)誤原因排查和解決辦法總結(jié)
這篇文章主要給大家介紹了NGINX 502錯(cuò)誤排查,辦法總結(jié),Nginx 413錯(cuò)誤的排查以及Nginx 400錯(cuò)誤排查,文中通過代碼示例給出了詳細(xì)的排查方法和解決方案,需要的朋友可以參考下2023-12-12nginx學(xué)習(xí)總結(jié)五(nginx反向代理)
Nginx代理與負(fù)載均衡配置與優(yōu)化技巧,方便需要的朋友2012-11-11Nginx服務(wù)器對數(shù)據(jù)傳輸速度限制的基本配置方法講解
這篇文章主要介紹了Nginx服務(wù)器對數(shù)據(jù)傳輸速度限制的基本配置方法講解,包括第三方開發(fā)的限速模塊Nginx-limit-traffic-rate-module的使用介紹,需要的朋友可以參考下2016-01-01