一、Nginx访问控制
1、nginx的访问模块:
(1)基于IP的访问控制:http_access_module
(2)基于用户的信任登录:http_auth_basic_module
1、基于IP的访问控制:
1、语法配置
语法:allow | deny +address
默认是无
作用的模块:http server location
2、配置测试
修改nginx的配置文件,我的在/etc/nginx/conf.d/nginx.conf
server {
listen 80;
server_name localhost;
location ~ ^/admin {
root /home/www/html;
index index.html index.hml;
deny 192.168.43.132;
allow all;
#deny 192.168.1.8;
}
}
#需要注意:
如果先允许访问,在定义拒绝访问。那么拒绝访问不生效。
同时也可以使用IP网段的配置方式,如`allow 192.168.1.0/24;`,表示满足此网段的IP都可以访问。
3、指定的location拒绝所有请求
如果你想拒绝某个指定URL地址的所有请求,而不是仅仅对其限速,只需要在location
块中配置deny
all指令:同样在配置文件中修改
server {
listen 80;
server_name localhost;
location /foo.html {
root /home/www/html;
deny all;
}
}
2、基于用户的信任登录:
1、配置语法
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
file:存储用户名密码信息的文件。
2、修改nginx的配置文件
server {
listen 80;
server_name localhost;
location ~ ^/admin {
root /home/www/html;
index index.html index.hml;
auth_basic "Auth access test!";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
auth_basic
不为off
,开启登录验证功能,auth_basic_user_file
加载账号密码文件。
3、建立口令文件
[root@proxy-nginx ~]# yum install -y httpd-tools
[root@proxy-nginx ~]# htpasswd -cm /etc/nginx/auth_conf user10
[root@proxy-nginx ~]# htpasswd -m /etc/nginx/auth_conf user20
[root@proxy-nginx ~]# cat /etc/nginx/auth_conf
user10:$apr1$MOa9UVqF$RlYRMk7eprViEpNtDV0n40
user20:$apr1$biHJhW03$xboNUJgHME6yDd17gkQNb0