nginx控制用户访问主要有三种方法:1:通过IP限制(配置allow和deny,属于ngx_http_access_module模块,配置段http, server, location, limit_except); 2、通过用户名密码限制(配置auth_basic和auth_basic_user_file,属于
ngx_http_auth_basic_module模块,配置段
http, server, location, limit_except );3、两种方法同时用。4、限制IP地址段(配置Geo,本文不配)
pid nginx.pid;
worker_processes 1;
events {
use epoll;
worker_connections 65535;
}
http{
gzip on;
gzip_min_length 1000;
......
server {
listen 80;
server_name localhost;
.......
#新增下面两行
auth_basic "Please input password"; #这里是验证时的提示信息
auth_basic_user_file /usr/local/src/nginx/passwd;
location /{
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
.......
}
}
}
上面将配置auth_basic和auth_basic_user_file放在server中,是对整个站点开启验证。若果要对部分开启验证,要凡在要验证的location中。allow和deny是按从上到下的顺序,类似iptables,匹配到了便跳出。