工作原因,需要對一個服務做做安全驗證,基于nginx負載:
nginx基于htpasswd安全驗證
在nginx 做安全驗證,在nginx.conf中server標簽下添加如下:
auth_basic "Authorized Login1111";
auth_basic_user_file /opt/nginx/conf/htpasswd;
也可以做location 匹配,依照個人需求
location / {
auth_basic "Authorized Login1111";
auth_basic_user_file /opt/nginx/conf/htpasswd;
}
然后使用htpasswd生成用戶和密碼(密文)
/usr/bin/htpasswd -c /opt/nginx/conf/htpasswd user
New password: ***
Re-type new password: ***
cat /opt/nginx/conf/htpasswd user
user:pwYOfUEu2Zrls
如果沒有htpasswd可以使用類似命令來操作
a=`perl -e 'print crypt($ARGV[0], "pwdsalt")' 123456`
echo "user:$a" >/opt/nginx/conf/htpasswd
cat /opt/nginx/conf/htpasswd user
user:pwy1aR9NQZei6
也可以寫成小腳本使用