极客分享 极客分享
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 算法知识
  • LeetCode题解
  • 一本通题解
  • ISBN公开信息查询接口
  • 认领打赏
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
打赏
关于
收藏
  • 分类
  • 标签
  • 归档

极客分享

Hosea & Harper
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 算法知识
  • LeetCode题解
  • 一本通题解
  • ISBN公开信息查询接口
  • 认领打赏
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
打赏
关于
收藏
  • 分类
  • 标签
  • 归档
  • nginx

    • Nginx 通过 Lua + Redis 实现动态封禁 IP
      • 安装 Nginx+Lua模块
      • 配置 Nginx 示例:
      • 更新ip_blacklist
      • 总结
  • redis

  • 后端
  • nginx
hosea
2021-11-24

Nginx 通过 Lua + Redis 实现动态封禁 IP

# Nginx 通过 Lua + Redis 实现动态封禁 IP

为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单。对于黑名单之内的 IP ,拒绝提供服务。

# 安装 Nginx+Lua模块

推荐使用 OpenResty,这是一个集成了各种 Lua 模块的 Nginx 服务器

安装方法在OpenResty官网 (opens new window)

我用的是Ubuntu server,这里记录一下:

你可以在你的 Ubuntu 系统中添加我们的 APT 仓库,这样就可以便于未来安装或更新我们的软件包(通过 apt-get update 命令)。 运行下面的命令就可以添加仓库(每个系统只需要运行一次):

  1. 安装导入 GPG 公钥时所需的几个依赖包(整个安装过程完成后可以随时删除它们):
sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates
1
  1. 导入 GPG 密钥:
wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
1
  1. 添加官方 APT 仓库

对于 x86_64 或 amd64 系统,可以使用下面的命令:

echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" \
    | sudo tee /etc/apt/sources.list.d/openresty.list
1
2
  1. 更新 APT 索引 并通过 APT 安装openresty:
sudo apt-get update
sudo apt-get -y install openresty
1
2

ps: 这个包同时也推荐安装 openresty-opm 和 openresty-restydoc 包,所以后面两个包会缺省安装上。 如果你不想自动关联安装,可以用下面方法关闭自动关联安装

sudo apt-get -y install --no-install-recommends openresty
1

# 配置 Nginx 示例:

Nginx 配置

http{
    lua_share_dict ip_blacklist 1m;
    server{
        listen 80;
        servername localhost;
        access_by_lua_file lua/ip_blacklist.lua;
        location / {
            root /opt/webroot;
        	index index.html index.htm;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

其中

lua_shared_dict ip_blacklist 1m;
1

由 Nginx 进程分配一块 1M 大小的共享内存空间,用来缓存 IP 黑名单,参见:

access_by_lua_file lua/ip_blacklist.lua;
1

ip_blacklist.lua文件内容如下:

-- a quick LUA access script for nginx to check IP addresses against an
-- `ip_blacklist` set in Redis, and if a match is found send a HTTP 403.
--
-- allows for a common blacklist to be shared between a bunch of nginx
-- web servers using a remote redis instance. lookups are cached for a
-- configurable period of time.
--
-- block an ip:
--   redis-cli SADD ip_blacklist 10.1.1.1
-- remove an ip:
--   redis-cli SREM ip_blacklist 10.1.1.1
--
-- also requires lua-resty-redis from:
--   https://github.com/agentzh/lua-resty-redis
--
-- your nginx http context should contain something similar to the
-- below: (assumes resty/redis.lua exists in /etc/nginx/lua/)
--
--   lua_package_path "/etc/nginx/lua/?.lua;;";
--   lua_shared_dict ip_blacklist 1m;
--
-- you can then use the below (adjust path where necessary) to check
-- against the blacklist in a http, server, location, if context:
--
-- access_by_lua_file /etc/nginx/lua/ip_blacklist.lua;
--
-- from https://gist.github.com/chrisboulton/6043871
-- modify by Ceelog

local redis_host    = "127.0.0.1"
local redis_port    = 6379

-- connection timeout for redis in ms. don't set this too high!
local redis_connection_timeout = 100

-- check a set with this key for blacklist entries
local redis_key     = "ip_blacklist"

-- cache lookups for this many seconds
local cache_ttl     = 60

-- end configuration

local ip                = ngx.var.remote_addr
local ip_blacklist      = ngx.shared.ip_blacklist
local last_update_time 	= ip_blacklist:get("last_update_time");

-- only update ip_blacklist from Redis once every cache_ttl seconds:
if last_update_time == nil or last_update_time < ( ngx.now() - cache_ttl ) then

  local redis = require "resty.redis";
  local red = redis:new();

  red:set_timeout(redis_connect_timeout);

  local ok, err = red:connect(redis_host, redis_port);
  if not ok then
    ngx.log(ngx.DEBUG, "Redis connection error while retrieving ip_blacklist: " .. err);
  else
    local new_ip_blacklist, err = red:smembers(redis_key);
    if err then
      ngx.log(ngx.DEBUG, "Redis read error while retrieving ip_blacklist: " .. err);
    else
      -- replace the locally stored ip_blacklist with the updated values:
      ip_blacklist:flush_all();
      for index, banned_ip in ipairs(new_ip_blacklist) do
        ip_blacklist:set(banned_ip, true);
      end

      -- update time
      ip_blacklist:set("last_update_time", ngx.now());
    end
  end
end

if ip_blacklist:get(ip) then
  ngx.log(ngx.DEBUG, "Banned IP detected and refused access: " .. ip);
  return ngx.exit(ngx.HTTP_FORBIDDEN);
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

# 更新ip_blacklist

在 Redis 服务器上新建 Set 类型的数据ip_blacklist,并加入最新的 IP 黑名单。

完成以上步骤后,重新加载 nginx,配置便开始生效了

这时访问服务器,如果你的 IP 地址在黑名单内的话,将出现拒绝访问:

image-20211124090354820

# 总结

以上,便是 Nginx+Lua+Redis 实现的 IP 黑名单功能,具有如下优点:

1、配置简单、轻量,几乎对服务器性能不产生影响;

2、多台服务器可以通过Redis实例共享黑名单;

3、动态配置,可以手工或者通过某种自动化的方式设置 Redis 中的黑名单。



如果你觉得我写的东西对你有用,想表示感谢的,扫下面的二维码吧😎。

微信 支付宝 云闪付(国内银行APP也可)
查看打赏名单
#后端#运维#nginx#redis
上次更新: 2021/11/26, 10:27:55
Redis数据迁移

Redis数据迁移→

最近更新
01
认领打赏
04-24
02
已认领打赏列表
04-24
03
1283:登山
03-11
更多文章>
Theme by Vdoing | Copyright © 2016-2023 极客分享

皖ICP备15004818号-3
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式