Summary: Most server administrators instinctively reach for Nginx’s limit_req module to stop HTTP floods. Here is how an unexpected use case for UBAN (GitHub) lets you build a smarter, status-code-aware, kernel-level rate limiter, and why attempting this in Fail2ban turns a simple rule into a heavy, over-engineered setup.

I built UBAN, a lightweight, native C intrusion prevention daemon that tails server logs in real time and drops malicious IPs directly at the kernel level via nftables. It was built for high-speed pattern matching and instant blocking; it was never designed to do rate limiting.

Recently, though, I realized that rate limiting is actually an unexpectedly great use case for UBAN.

Infographic illustrating UBAN kernel-level HTTP rate limiting vs Nginx limit_req, showing how UBAN reads Nginx access logs in native C and drops scraper traffic at the kernel level via nftables within 5ms

Why I Never Liked Nginx Rate Limiting

I have never liked Nginx’s built-in rate limiting (limit_req), and I don’t use it on my servers.

To configure it properly, you have to carve out shared memory zones in nginx.conf, guess at rate and burst buckets, and decide between delaying requests or rejecting them with HTTP 429/503 errors. Worse still, Nginx’s rate limiter has two major flaws when dealing with real-world scrapers and bots:

  • It Treats Every Request Blindly: Nginx counts raw requests per second without caring what happened. A legitimate visitor loading 12 cached images on a heavy blog post looks identical to a scraper hammering 12 missing endpoints.
  • It Has Short-Term Amnesia & Wastes CPU: Even when Nginx throttles a bot with a 503 or 429, Nginx still has to accept the TCP connection, evaluate the memory bucket, send back an HTTP response, and write to the error log. Once the bot slows down or pauses long enough for its excess-request counter to decay, it can resume sending requests without any persistent penalty. Nginx rate limiting regulates traffic; it doesn’t remember repeat offenders or escalate penalties.

That’s when it clicked: why manage rate limiting inside the web server at all when UBAN is already watching the access log in real time?


Measuring Real Human Traffic First

Before setting a rate limit, you need to know what normal human browsing actually looks like on your site.

My site is relatively lightweight. I opened a clean guest browser profile (cold cache) and clicked straight into the most image-heavy blog post on this entire domain. Looking at the Nginx access log, that single page load generated 13 HTTP 200 requests within one second. Browsing most other pages generated 10 or fewer requests per second. On repeat visits, local browser caching reduces those numbers even further.

Since a human reader cannot realistically load a heavy page and click into another one within the exact same 1-second window, any IP generating 20 or more requests in 1 second is almost certainly an automated scraper, scanner, or flood script.


The UBAN Rate-Limiter Configuration

Assuming you have already placed $status at Position 2 in your Nginx log format (as covered in the UBAN Technical Deep Dive), setting up intelligent rate limiting takes just one jail block in /etc/uban/uban.conf and a command sudo systemctl reload uban:

[nginx-ratelimit]
enabled = true
log_path = "/var/log/nginx/access.log"
ip_pos = 1
match_pos = 2
match_string = "200", "204", "206", "301", "302", "304", "400", "401", "403", "404", "405", "408", "444", "499", "500", "502"
max_retry = 20
find_time = 1
ban_time = 300
banfactor = 2
max_bantime = 28d

Notice what makes this smarter than standard web-server rate limiting: you choose which HTTP status codes count. Here, the jail tracks 16 high-signal status codes across normal page loads (200, 204, 206), redirects and cache checks (301, 302, 304), selected client errors and scanner responses (including Nginx’s 444 and 499), and upstream failures (500, 502), while allowing you to omit anything noisy or internal like 429 or 503.

Better yet, you don’t have to stop at a single time window. Because a human visitor exhibits a brief burst when loading a page and then goes quiet while reading, you can stack additional jails with identical settings but wider windows and lower per-second averages; for example, pairing a find_time = 1 / max_retry = 20 burst jail (20 req/s) with a find_time = 2 / max_retry = 30 jail (15 req/s) and a find_time = 3 / max_retry = 40 jail (~13.3 req/s). This multi-window envelope lets legitimate bursts pass untouched while catching throttled crawlers that pace themselves just under a 1-second ceiling.


Why This Beats Nginx (and Why Fail2ban Gets Heavy)

🛡️ Kernel Drop vs. HTTP 429

Once an IP crosses the threshold, UBAN blocks it at the kernel firewall level through nftables. Subsequent traffic is dropped before it reaches Nginx, eliminating HTTP request-processing and response-generation overhead for that offender.

📈 Escalating Bans

Unlike Nginx’s stateless buckets, UBAN tracks repeat offenders in SQLite and escalates their ban times automatically if they come back.

I came up with this idea completely independently and already had it deployed and tested on my production server before I ever searched to see if anyone else had tried it. Afterward, out of curiosity, I looked around online and found a great write-up on Schallbert’s Blog documenting a similar approach with Caddy and Fail2ban by filtering HTTP 200 responses. (Yes, sometimes I do like taking credit!) Interestingly, that author ran into the exact same static-asset inflation issue (two page clicks generating 35 log entries), ended up configuring Caddy’s log_skip /assets* to exclude asset hits from the logs, and wrestled with separate filter files, custom regexes, Unix timestamp date patterns, and Docker iptables chain overrides just to get a single rate-limit jail working.

That highlights the real trade-off when trying to turn Fail2ban into an access-log rate limiter: setup complexity and regex overhead. Because Fail2ban is designed around regex-based log filters, turning it into a general-purpose rate limiter means evaluating those filters against a continuous stream of ordinary access-log entries, including successful page views and asset requests. That introduces additional parsing and regex-processing overhead, along with considerably more configuration complexity.

UBAN, on the other hand, evaluates the status code at a fixed position using native C string matching, turning your log-banning daemon into a lightweight, kernel-level rate limiter with just a few lines of config.

After deploying the jail, you should audit how it behaves against real traffic by running uban status nginx-ratelimit -d to inspect both active and historical bans. If any legitimate visitors get caught (for example, someone opening multiple tabs at once), compare the flagged timestamps in /var/log/uban/uban.log against /var/log/nginx/access.log, lift any false positive with sudo uban unban <IP>, and adjust your max_retry or find_time thresholds accordingly.