Nginx实现访问控制

一、Nginx访问控制模块

实现Nginx的访问控制有两种方式:基于IP的访问控制和基于Basic Auth 认证的访问控制。

1.1 基于IP的访问控制

  • 访问控制模块:http_access_module

    nginx中内置ngx_http_access_module,可通过设置allow和deny来实现IP的访问控制。

  • 配置语法

    编辑/usr/local/nginx/conf/nginx.conf配置文件。

    Syntax:allow address | all;
    default:默认无
    Context:http,server,location
    Syntax:deny address | all;
    default:默认无
    Context:http,server,location
    

    allow 允许 //ip或者网段
    deny 拒绝 //ip或者网段

1.2 基于Basic Auth 认证

  • 访问控制模块:http_auth_basic_module

    Nginx提供HTTP的Basic Auth功能,配置了Basic Auth之后,需要输入正确的用户名和密码之后才能正确的访问网站。我们使用htpasswd来生成密码信息,首先要安装httpd-tools,在httpd-tools中包含了htpasswd命令。

  • 配置语法

    编辑/usr/local/nginx/conf/nginx.conf配置文件。

    Syntax:auth_basic string | off;
    default:auth_basic off;
    Context:http,server,location
    
    Syntax:auth_basic_user_file file;
    default:默认无
    Context:http,server,location
    file:存储用户名密码信息的文件。
    

二、基于IP的访问控制实战

2.1 allow

(1)编辑Nginx配置文件

查看一下我宿主机IP。

在这里插入图片描述

server {
       listen       80;
       server_name  localhost;

            location / {
            root   html;
            index  index.html index.htm;
            deny 192.168.0.100;    #拒绝该IP访问我的Nginx服务器
            allow all;
            }
}

实现功能:

  • 宿主机IP为192.168.0.100,虚拟机IP为192.168.0.106,故这里禁止宿主机访问,允许其他所有IP访问。
  • 当宿主机访问http://192.168.0.106时,显示403 Forbidden
  • 当然也可以反向配置,同时也可以使用IP网段的配置方式,如allow 192.168.0.0/24;表示满足此网段的IP都可以访问。

需要注意:

  • 按顺序匹配,已经被匹配的ip或者网段,后面不再被匹配。
  • 如果先允许所有ip访问,在定义拒绝访问。那么拒绝访问不生效。
  • 默认为allow all
[root@zrs conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zrs conf]# /usr/local/nginx/sbin/nginx -s reload

(2)浏览器端访问

可见权限被拒,返回403状态码。

在这里插入图片描述

2.2 deny

如果你想拒绝某个指定URL地址的所有请求,只需要在 location 块中配置 deny all 指令即可。

server {
       listen       80;
       server_name  localhost;

            location / {
            root   html;
            index  index.html index.htm;
            deny all;  #拒绝所有客户端访问
            }
}
[root@zrs conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zrs conf]# /usr/local/nginx/sbin/nginx -s reload

三、基于Basic Auth认证的访问控制实战

有时我们会有这么一种需求,就是你的网站的某些页面不希望公开,我们希望的是某些特定的客户端可以访问。那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那些不速之客。

基于用户的信任登录模块:http_auth_basic_module

(1)编辑配置文件/usr/local/nginx/conf/nginx.conf

server {
       listen       80;
       server_name  localhost;

            location / {
            root   html;
            index  index.html index.htm;
            auth_basic "Auth access test!";
		    auth_basic_user_file /usr/auth_conf;
            }
}

auth_basic不为off,开启登录验证功能,auth_basic_user_file加载账号密码文件。

(2)建立口令文件

htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件。

[root@zrs ~]# yum install -y httpd-tools
[root@zrs ~]# htpasswd -cm /etc/nginx/auth_conf user1   #-c:创建解密文件;-m:MD5加密
[root@zrs ~]# htpasswd -m /etc/nginx/auth_conf user2
[root@zrs ~]# cat /etc/nginx/auth_conf
user1:$apr1$RKPoSilw$Bu2gK7LEhQVjKA/5KsLht/
user2:$apr1$w.//6E7A$7ce/.P4FceOwmrlLtw/TQ/

(3)浏览器端访问

在这里插入图片描述

在这里插入图片描述
以上操作均实现了访问控制的效果,这对于我们的运维安全是由一定的实际效果的。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云计算-Security

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值