M
MD Rakibul Islam Rakib
Guest
On 3 November, at 10:02 UTC, most of my website started returning 500 errors. Service pages, project pages, blog posts: anything that needed data from my API was broken. The API itself was fine. Its process was running and its health check passed.
Thirteen minutes later it was fixed, and the culprit turned out to be my own security setup, triggered by my own SEO crawler. This is what happened, how I reproduced it, and the second problem I found while reproducing it, which was still live on my server after the fix.
My site is a Next.js frontend and a NestJS API on one Linux server, with Cloudflare in front of both. The API is protected the usual way:
There's one detail that matters a lot here. When the frontend renders a page on the server, it calls the API through its public URL, which goes through Cloudflare. So the API sees frontend traffic arriving from Cloudflare, just like visitor traffic.
I had just deployed, and I ran my usual SEO crawl across the site to check for broken links. It's fast: hundreds of pages in a short time, and every page makes the frontend call the API.
Behind Cloudflare, nginx doesn't see the visitor's IP address. It sees the address of the Cloudflare edge server that forwarded the request. All of that crawl traffic, plus every real visitor routed through the same edges, plus my own frontend's calls, arrived from a handful of Cloudflare IPs.
To nginx, that looked like four clients sending far too many requests. It rate-limited them. fail2ban read the log, counted, and did exactly what I'd told it to do: it banned four Cloudflare edge IP addresses at the firewall.
From then on, every request through those edges was refused at the firewall. Cloudflare couldn't reach my API, so it answered my frontend with a 521, and my frontend turned that into a 500 for the visitor. My security layer had locked out the CDN that sits in front of my own site.
The immediate fix was to unban the four IPs. The real fix took one more step.
I didn't want to write about this from memory, so I rebuilt it in Docker: an API container running nginx and fail2ban with the same limits as production, and a second container at a fixed IP (
The first thing I learned had nothing to do with Cloudflare. My test endpoint was a one-liner:
I fired 300 parallel requests at it, and all 300 came back 200. No rate limiting at all. nginx's
With that fixed, the crawler sent 300 parallel requests through the edge:
That's 60 requests of burst plus one, then 429s. A few seconds later, fail2ban acted:
It banned the edge, not the crawler. Then the "frontend" tried its normal request through the same edge, with its own real IP in the header:
That's the incident in three lines.
nginx's
Cloudflare publishes its ranges at
In the lab I trusted the stand-in edge's network and ran the crawl again. This time the limiter and the ban landed on the right address:
And the frontend kept working:
This is the fix I applied in production on 3 November. The access log started showing real visitor addresses, and the server's own address was already on the exempt list.
Then I sent the banned crawler back in, 100 more parallel requests:
It was "banned", and it got served anyway.
fail2ban had put
My production server uses fail2ban's
So after my fix, the bans on my production jail (it shows 21 since fail2ban last started) only work against clients that connect to the server directly. For anything that comes through Cloudflare, which is almost all real traffic, they are decorative. Nobody would ever notice, because fail2ban reports success and the rate limiter quietly does the real work.
If the real IP only exists inside nginx, ban it inside nginx. A fail2ban action can write
With
The frontend stayed up, and
The other good option is to ban at Cloudflare itself with an action that calls Cloudflare's API, so the visitor is blocked at the edge before it reaches you at all. fail2ban ships a
It took 13 minutes to fix the outage and an afternoon in a lab to understand it properly. The lab found the bigger problem. The outage was loud and obvious; the decorative ban had been running silently ever since my fix.
Thirteen minutes later it was fixed, and the culprit turned out to be my own security setup, triggered by my own SEO crawler. This is what happened, how I reproduced it, and the second problem I found while reproducing it, which was still live on my server after the fix.
The setup
My site is a Next.js frontend and a NestJS API on one Linux server, with Cloudflare in front of both. The API is protected the usual way:
- nginx
limit_req: 20 requests per second per client, with a burst of 60. Anything beyond that gets a 429. - A fail2ban jail on nginx's error log: if a client is rate-limited 20 times within 10 minutes, it's banned for an hour.
There's one detail that matters a lot here. When the frontend renders a page on the server, it calls the API through its public URL, which goes through Cloudflare. So the API sees frontend traffic arriving from Cloudflare, just like visitor traffic.
What happened
I had just deployed, and I ran my usual SEO crawl across the site to check for broken links. It's fast: hundreds of pages in a short time, and every page makes the frontend call the API.
Behind Cloudflare, nginx doesn't see the visitor's IP address. It sees the address of the Cloudflare edge server that forwarded the request. All of that crawl traffic, plus every real visitor routed through the same edges, plus my own frontend's calls, arrived from a handful of Cloudflare IPs.
To nginx, that looked like four clients sending far too many requests. It rate-limited them. fail2ban read the log, counted, and did exactly what I'd told it to do: it banned four Cloudflare edge IP addresses at the firewall.
From then on, every request through those edges was refused at the firewall. Cloudflare couldn't reach my API, so it answered my frontend with a 521, and my frontend turned that into a 500 for the visitor. My security layer had locked out the CDN that sits in front of my own site.
The immediate fix was to unban the four IPs. The real fix took one more step.
Reproducing it
I didn't want to write about this from memory, so I rebuilt it in Docker: an API container running nginx and fail2ban with the same limits as production, and a second container at a fixed IP (
172.31.0.10) standing in for a Cloudflare edge. Both the "crawler" and the "frontend" send their requests through that edge, each with a CF-Connecting-IP header carrying its real address, just like Cloudflare does.The first thing I learned had nothing to do with Cloudflare. My test endpoint was a one-liner:
Code:
location / {
limit_req zone=api burst=60 nodelay;
return 200 "ok from api\n";
}
I fired 300 parallel requests at it, and all 300 came back 200. No rate limiting at all. nginx's
return runs in the rewrite phase, which comes before the phase where limit_req is checked, so the request is answered before the limiter ever looks at it. If you test rate limits against a return location, they will look broken. Serving a file instead (try_files /ok.txt =404) made the limiter work.With that fixed, the crawler sent 300 parallel requests through the edge:
Code:
61 200
239 429
That's 60 requests of burst plus one, then 429s. A few seconds later, fail2ban acted:
Code:
`- Banned IP list: 172.31.0.10
Code:
-A f2b-api -s 172.31.0.10/32 -j REJECT --reject-with icmp-port-unreachable
It banned the edge, not the crawler. Then the "frontend" tried its normal request through the same edge, with its own real IP in the header:
Code:
curl: (7) Failed to connect to 172.31.0.2:80 after 0 ms: Could not connect to server
That's the incident in three lines.
The real fix: tell nginx who the visitor is
nginx's
realip module replaces the connecting address with the one in a header, but only for requests from addresses you trust:
Code:
set_real_ip_from 173.245.48.0/20; # ...one line per Cloudflare range
set_real_ip_from 2400:cb00::/32;
real_ip_header CF-Connecting-IP;
Cloudflare publishes its ranges at
cloudflare.com/ips-v4 and /ips-v6. They rarely change, but a cron job that rebuilds this file and runs nginx -t && systemctl reload nginx is cheap insurance. Never trust the header from everywhere: anyone who can reach your origin directly could then claim any IP they like.In the lab I trusted the stand-in edge's network and ran the crawl again. This time the limiter and the ban landed on the right address:
Code:
`- Banned IP list: 198.51.100.7
Code:
198.51.100.7 cf=198.51.100.7 "GET /blogs?page=254 HTTP/1.1" 429
And the frontend kept working:
Code:
frontend: ok from api
This is the fix I applied in production on 3 November. The access log started showing real visitor addresses, and the server's own address was already on the exempt list.
The second problem: the ban that does nothing
Then I sent the banned crawler back in, 100 more parallel requests:
Code:
crawler again (banned in iptables):
61 200
39 429
It was "banned", and it got served anyway.
fail2ban had put
198.51.100.7 into the firewall. But the firewall never sees 198.51.100.7. Packets still arrive from the Cloudflare edge; the visitor's address only exists inside an HTTP header, which the firewall doesn't read. The ban blocks an address that never connects. nginx's own rate limit still slowed the crawler down, but the ban itself did nothing.My production server uses fail2ban's
nftables action (the Debian/Ubuntu package's defaults-debian.conf sets banaction = nftables), not iptables, so I repeated the test with nftables. Same result: the address went into the set, and the crawler was still served.
Code:
set addr-set-api {
type ipv4_addr
elements = { 198.51.100.7 }
}
So after my fix, the bans on my production jail (it shows 21 since fail2ban last started) only work against clients that connect to the server directly. For anything that comes through Cloudflare, which is almost all real traffic, they are decorative. Nobody would ever notice, because fail2ban reports success and the rate limiter quietly does the real work.
Banning where the real IP exists
If the real IP only exists inside nginx, ban it inside nginx. A fail2ban action can write
deny lines into a file that nginx includes:
Code:
# /etc/fail2ban/action.d/nginx-deny.conf
[Definition]
actionban = echo "deny <ip>;" >> /etc/nginx/fail2ban-deny.conf && nginx -s reload
actionunban = sed -i "/^deny <ip>;$/d" /etc/nginx/fail2ban-deny.conf && nginx -s reload
Code:
server {
include /etc/nginx/fail2ban-deny.conf;
...
}
With
banaction = nginx-deny in the jail, the crawl produced a ban, the file got deny 198.51.100.7;, and the crawler's next 100 requests came back as:
Code:
61 403
39 429
The frontend stayed up, and
fail2ban-client set api unbanip removed the line again. (The 429s are still there because nginx checks the rate limit before the deny rule.)The other good option is to ban at Cloudflare itself with an action that calls Cloudflare's API, so the visitor is blocked at the edge before it reaches you at all. fail2ban ships a
cloudflare action for this. The nginx file is simpler to set up and has no API token to protect; the Cloudflare action also saves your server the work.What I would tell anyone behind a CDN
- Set the real IP before you rate-limit or ban anything. Without
set_real_ip_fromandreal_ip_header, every limit and every ban applies to your CDN. Check your access log: if it's full of the same few addresses, that's you. - Add the CDN's ranges to fail2ban's
ignoreipas a safety net, so a config mistake can never ban the edge again. - Know where your bans actually happen. A firewall ban only works on traffic that connects directly. Behind a proxy, ban in nginx or at the CDN.
- Alert on bans you don't expect. A one-line check that warns when fail2ban bans an address inside a Cloudflare range would have told me what was wrong within a minute, instead of me working backwards from 500s.
- Remember your own traffic. My frontend calls the API through the CDN, so to the API it looks like just another visitor. Exempt it by address, or call the API over localhost.
- Re-check after control-panel changes. My server's vhosts are managed by a hosting panel, which can rewrite the file and silently drop the
realiplines. I keep a note to re-add them.
It took 13 minutes to fix the outage and an afternoon in a lab to understand it properly. The lab found the bigger problem. The outage was loud and obvious; the decorative ban had been running silently ever since my fix.