How to Build a Dynamic IP Blacklist with Nginx, Lua, and Redis

Learn to protect your server from malicious crawlers by creating a dynamic IP blacklist using Nginx, Lua, and Redis, covering architecture choices, installation steps, configuration snippets, and how to share and update the blacklist across multiple machines.

Open Source Linux
Open Source Linux
Open Source Linux
How to Build a Dynamic IP Blacklist with Nginx, Lua, and Redis

Background

To block certain crawlers or malicious users from accessing the server, we need to build a dynamic IP blacklist. Requests from IPs on the blacklist are denied.

Architecture

There are several ways to implement an IP blacklist:

Configure iptables at the operating system level to reject traffic from specific IPs.

Use Nginx's deny directive or a Lua plugin at the web server level.

Check the client IP in the application layer before processing the request.

For easy management and sharing, we choose an Nginx + Lua + Redis architecture.

Implementation

Install Nginx with Lua support. Using OpenResty is recommended because it bundles many useful Lua modules.

Install and start a Redis server.

Configure Nginx with the following snippets: lua_shared_dict ip_blacklist 1m; This allocates a 1 MB shared memory zone for caching the IP blacklist. access_by_lua_file lua/ip_blacklist.lua; This tells Nginx to run the specified Lua script for each request.

Write a Lua script that periodically fetches the latest blacklist from Redis.

Create a Redis Set named ip_blacklist and populate it with the IPs to block.

After completing these steps, reload Nginx. Requests from IPs present in the Redis set will be denied, as shown below:

Conclusion

The Nginx + Lua + Redis solution for an IP blacklist offers several advantages:

Simple, lightweight configuration with negligible performance impact.

Multiple servers can share the same blacklist via a central Redis instance.

Dynamic updates are possible manually or through automation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendredisNginxLuaServer Securityip blacklist
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.