Donate to support Ukraine's independence.
Fail2ban reading Nginx logs and banning attackers in Cloudflare

Auto-Ban Attackers in Cloudflare Using Fail2ban and Nginx Logs

Many of us use CloudFlare to protect our websites and services from malicious activity. In this post, I’ll show you how to configure Fail2ban to block bad actors directly on CloudFlare. Nginx setup Before we start, make sure your Nginx logs contain real client IP addresses. If your logs still show CloudFlare IPs, follow my earlier guide: Handling Real IP in Nginx Behind Load Balancer and Cloudflare ...

November 22, 2025 · Last Modified: October 21, 2026 · 3 min · 547 words · Serhii Kaidalov
Client traffic passing through Cloudflare and an NLB to Nginx, restoring the real client IP

Why Nginx Logs Wrong Client IPs Behind Cloudflare and NLB (and How to Fix It)

When running a website behind a load balancer and Cloudflare, handling client IPs in Nginx can be tricky. Different setups use different headers to pass the real IP: Custom Domains via Cloudflare: Cloudflare sends the real client IP in the CF-Connecting-IP header. Direct CNAME to Load Balancer: The load balancer sets the real client IP in the X-Forwarded-For header. What is the challenge? Nginx’s real_ip_header directive doesn’t support variables. This limitation means you can’t conditionally choose between headers like X-Forwarded-For and CF-Connecting-IP. ...

December 28, 2024 · Last Modified: September 29, 2026 · 3 min · 527 words · Serhii Kaidalov