Information Security 4 min read

Implementing a Dynamic IP Blacklist with Nginx, Lua, and Redis

This guide explains how to build a dynamic IP blacklist using Nginx, Lua scripts, and Redis to block malicious or unwanted requests at the server level, covering architecture choices, installation steps, configuration details, and the benefits of a lightweight, shared, and easily updatable solution.

Architecture Digest
Architecture Digest
Architecture Digest
Implementing a Dynamic IP Blacklist with Nginx, Lua, and Redis

Background – To block crawlers or malicious users, a dynamic IP blacklist is needed so that requests from blacklisted IPs are denied.

Architecture – Several approaches exist (iptables, Nginx deny rules, application‑level checks). The chosen solution combines Nginx, Lua, and Redis, allowing centralized management and sharing of the blacklist across multiple servers.

Implementation steps

Install Nginx with Lua support, preferably using OpenResty.

Install and start a Redis server.

Configure Nginx with the necessary directives.

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.

Nginx configuration

Define a shared memory zone for the blacklist:

lua_shared_dict ip_blacklist 1m;

Specify the Lua script to run on each request:

access_by_lua_file lua/ip_blacklist.lua;

The shared memory caches the blacklist, and the Lua script checks the client IP against the Redis set before allowing access.

After reloading Nginx, any request from an IP present in the blacklist will be rejected, as shown in the example screenshots.

Conclusion

Simple and lightweight configuration with minimal performance impact.

Multiple servers can share the same blacklist via Redis.

Dynamic updates are possible manually or through automation by modifying the Redis set.

RedisDynamic Configurationnginxluaserver securityIP blacklist
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login 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.