|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
欢迎大家来到仓酷云论坛!一.需求形貌
Nginx作为webserver的办事。因营业必要在会见网站时,加一层认证信息,以避免公司以外的人会见网站。需求相似于Apache那样为指定的目次增加会见考证,一样平常在Apache下利用htpasswd来增加。这里是为Nginx增加。
二.编写以下步伐
情况:必要撑持perl。(由于用到perl)
vihtpasswd.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
exportPATH
echo"====================================="
echo"#AtoollikehtpasswdforNginx#"
echo"#-----------------------------------#"
echo"#Author:Licesshttp://blog.csdn.net/longxibendi#"
echo"====================================="
#setUserName
username=""
read-p"PleaseinputUserName:"username
if["$username"=""];then
echo"Error:UserNamecantbeNULL!"
exit1
fi
echo"==========================="
echo"UserNamewas:$username"
echo"==========================="
#setpassword
unpassword=""
read-p"PleaseinputthePassword:"unpassword
if["$unpassword"=""];then
echo"Error:PasswordcantbeNULL!"
exit1
fi
echo"==========================="
echo"Passwordwas:$unpassword"
echo"==========================="
password=$(perl-eprintcrypt($ARGV[0],"pwdsalt")$unpassword)
#sethtpasswdfile
htfile=""
read-p"PleaseinputAuthfilename:"htfile
if["$htfile"=""];then
echo"Error:AuthfilenamecantbeNULL!"
exit1
fi
echo"==========================="
echo"AuthFile:/longxibendi/nginx/conf/$htfile"
echo"==========================="
get_char()
{
SAVEDSTTY=`stty-g`
stty-echo
sttycbreak
ddif=/dev/ttybs=1count=12>/dev/null
stty-raw
sttyecho
stty$SAVEDSTTY
}
echo""
echo"PressanykeytoCreat...orPressCtrl+ctocancel"
char=`get_char`
if[!-f/longxibendi/nginx/conf/$htfile.conf];then
make-p/longxibendi/nginx/conf/$htfile.conf
echo"CreateAuthfile......"
cat>/longxibendi/nginx/conf/$htfile.conf<<eof
$username:$password
eof
echo"CreateAuthfilesuccessful,authfilepath:/longxibendi/nginx/conf/$htfile.conf."
else
echo"Filealreadyexists,pleaserunthisscriptagain."
exit1
fi
三.天生用户名暗码信息
bashhtpasswd.sh
按提醒输出用户名、暗码、及认证文件名。剧本会主动天生认证文件。纪录下剧本前往的文件路径。如:/longxibendi/nginx/conf/longxibendi.auth.conf
四.修正Nginx.conf设置文件,为Nginx增加auth认证设置
location^~/longxibendi/
{
auth_basic"Authorizedusersonly";
auth_basic_user_file/longxibendi/nginx/conf/longxibendi.auth.conf;#这里写后面剧本前往的文件路径;
}
大概在location前面增加,好比
location/{
indexindex.phpindex.htmlindex.htm;
if(!-f$request_filename){
rewrite^/(.*)/iphone/index.php?$1last;
}
}
location~.*.(php|php5)${
fastcgi_pass127.0.0.1:8000;
fastcgi_indexindex.php;
includefcgi.conf;
}
auth_basic"Authorizedusersonly";
auth_basic_user_file/longxibendi/nginx/conf/moyi.365.auth.conf;
五.重启Nginx
cdsbin/nginx;./nginx-sreload
六.注重事项
每一个扫瞄器初度会见,输出用户名、暗码。只必要输出一次,以后再翻开扫瞄器就不必要输出了。
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们! |
|