在上一章,介绍了对用户的访问限制,这章重点介绍介绍对用户访问目录进行加密,只有输入正确的用户名与密码才能进行访问。
这个功能特性是由ngx_http_auth_basic_module提供的,默认编译nginx时会编译好,主要有以下两个指令。
Syntax: auth_basic string | off;
Default:
auth_basic off;
Context: http, server, location, limit_except
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
认证的配置可以在http指令块,server指令块,location指令块配置。
auth_basic string :定义认证的字符串,会通过响应报文返给客户端,也可以通过这个指令关闭认证。 auth_basic_user_file file :定义认证文件。
实例:
对匹配目录加密:
location /img {
auth_basic "User Auth";
auth_basic_user_file /usr/local/nginx/conf/auth.passwd;
}
auth_basic 设置名字
auth_basic_user_file 用户认证文件放置路径
生成认证文件:
yum install httpd-tools -y
htpasswd -c /usr/local/nginx