nginx 访问控制
1.nginx 访问控制模块
(1)基于IP的访问控制:http_access_module
(2)基于用户的信任登录:http_auth_basic_module
2.基于IP的访问控制
2.1 配置语法
Syntax:allow address | all;
default:默认无
Context:http,server,locationSyntax:deny address | all;
default:默认无
Context:http,server,location
===================================================
allow 允许 //ip或者网段
deny 拒绝 //ip或者网段
2.2 测试(yum安装的nginx)
机器一:
[root@192 ~]# vim /etc/nginx/conf.d/access_mod.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.hml;
deny 192.168.1.8;
allow all;
}
}
[root@192 ~]# nginx -t
[root@192 ~]# nginx -s reload
192.168.1.8访问(curl -I http://机器一的ip)显示403错误
192.168.1.10访问(curl -I http://机器一的ip)显示200 OK
1.按顺序匹配,已经被匹配的ip或者网段,后面不再被匹配。
2.如果先允许所有ip访问,在定义拒绝访问。那么拒绝访问不生效。
3.默认为allow all
2.3 指定location拒绝所有请求
如果你想拒绝某个指定URL地址的所有请求,只需要在location块中配置deny all指令:
[root@192 ~]# vim /etc/nginx/conf.d/access_mod.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.hml;
deny all; #拒绝所有
}
}
[root@192 ~]# nginx -t
[root@192 ~]# nginx -s reload
/这个路径的请求拒绝
3. 基于用户的信任登录
基于用户的信任登录模块:http_auth_basic_module
有时我们会有这么一种需求,就是你的网站的某些页面不希望公开,我们希望的是某些特定的客户端可以访问。那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那些不速之客
3.1 配置语法
Syntax:auth_basic string | off;
default:auth_basic off;
Context:http,server,locationSyntax:auth_basic_user_file file;
default:默认无
Context:http,server,location
file:存储用户名密码信息的文件。
auth_basic不为off,开启登录验证功能,auth_basic_user_file加载账号密码文件。
3.2 配置示例
[root@192 ~]# vim /etc/nginx/conf.d/auth_mod.conf
server {
listen 80;
server_name localhost;
location ~ /admin {
root /var/www/html;
index index.html index.hml;
auth_basic "Auth access test!";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
[root@192 ~]# nginx -t
[root@192 ~]# nginx -s reload
这里也可以用systemctl start nginx
3.2.1 建立口令文件
[root@192 ~]# yum install -y httpd-tools
#htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件
[root@192 ~]# htpasswd -cm /etc/nginx/auth_conf user10
# -c 创建解密文件,-m MD5加密对密码
[root@192 ~]# htpasswd -m /etc/nginx/auth_conf user20
[root@192 ~]# cat /etc/nginx/auth_conf
user10:$apr1$MOa9UVqF$RlYRMk7eprViEpNtDV0n40
user20:$apr1$biHJhW03$xboNUJgHME6yDd17gkQNb0
3.2.2 测试

更多推荐



所有评论(0)