认证模块
默认情况下nginx已经安装了ngx_http_auth_basic_module模块,如果不需要这个模块,可以加上 --without-http_auth_basic_module 。ngx_http_auth_basic_module模块基于“HTTP Basic Authentication“协议完成用户认证。
例如:
location / {
auth_basic "welcome";
auth_basic_user_fileconf/htpasswd;
}
auth_basic_user_file指令:
语法:auth_basic_user_file file;
默认:–
用于指定保存用户名和密码的文件,注意文件权限。
文件格式为:
name1:password1
name2:password2
支持的密码类型
用crypt()函数加密,工具有htpasswd、openssl passwd
使用基于md5的密码算法的Apache变体(apr1)
使用htpasswd实现nginx的认证
yum install httpd-tools -y
htpasswd -h
htpasswd -c /etc/nginx/passwd.db xiaoming ###新创建密码文件
New password:
Re-type new password:
Adding password for user xiaoming
配置nginx
location / {
auth_basic "welcome";
auth_basic_user_file /etc/nginx/passwd.db;
}
网友评论