Skip to content

ModSecurity false positives behind Cloudflare on cPanel servers

Fix ModSecurity false positives on a cPanel server that sits behind Cloudflare, and stop the firewall from banning Cloudflare's own addresses. You will restore real visitor IPs, protect the edge from bans and then tune rules, without switching the WAF off.

If your cPanel server sits behind Cloudflare and some visitors suddenly get 403 or 522 errors while others load the site fine, ModSecurity is probably judging Cloudflare instead of your visitors. Every request reaches the server from a small pool of Cloudflare edge IPs, so rules that count or score per IP add up thousands of unrelated people as if they were one. That includes rate limits, repeat-offender scoring and LFD’s LF_MODSEC trigger. LFD (Login Failure Daemon, the blocking half of CSF, ConfigServer Security & Firewall) then firewalls an edge IP, and everyone routed through that edge is cut off. Fix it in this order: restore the real client IP first, make the firewall unable to ban Cloudflare second, and only then tune individual ModSecurity rules.

What’s happening

Two distinct failures stack on top of each other.

First, attribution collapse. Without mod_remoteip, every request’s REMOTE_ADDR is a Cloudflare IP. One real attacker probing through the edge generates ModSecurity hits “from” that edge IP; so does every innocent visitor routed through the same POP. Anything that counts hits per address — CRS DoS rules, cPanel’s hit-count integrations, LFD’s LF_MODSEC threshold — is now counting a crowd as one person.

Second, the enforcement layer makes it worse. When LFD sees enough ModSecurity hits from one address, it adds that address to /etc/csf/csf.deny. Ban a normal attacker and you win. Ban 172.70.x.x and you have firewalled part of Cloudflare: connections from that edge IP to your origin fail, Cloudflare returns 522 origin-unreachable errors for whichever visitors land on it, and the pattern looks maddeningly intermittent because it depends on which POP serves whom.

A third, quieter effect: Cloudflare’s own WAF and bot mitigation stop a share of hostile traffic at the edge, so what reaches ModSecurity is pre-filtered. That is good — but it means alert volume changes are expected after fronting, and “ModSec got quiet” is not evidence it broke.

What to check first

Establish whether the IPs generating hits are Cloudflare’s, and whether any have already been banned. Until both answers are known, do not touch rule configuration — a disabled rule cannot un-ban an edge IP.

How to verify

# Which client IPs are generating ModSecurity hits?
grep -oE 'client [0-9a-fA-F.:]+' /etc/apache2/logs/error_log | sort | uniq -c | sort -rn | head

# Is a hot IP a Cloudflare edge address, and is it already blocked?
csf -g 172.70.10.10

# Has LFD been banning based on ModSecurity triggers?
grep -i modsec /var/log/lfd.log | tail -20

# Full detail on specific hits
tail -100 /etc/apache2/logs/modsec_audit.log

Cross-check suspicious ranges against the published list at cloudflare.com/ips. Cloudflare edge IPs sitting in csf.deny, or lfd.log lines showing LF_MODSEC blocks of those ranges, confirm the full failure chain. WHM » Security Center » ModSecurity Tools » Hits List shows the same hits with the triggering rule IDs.

The fix

  1. Restore real client IPs with mod_remoteip. This is the prerequisite for everything else: once REMOTE_ADDR is the true visitor, per-IP rules go back to measuring individuals. Full walkthrough in restoring real visitor IPs on cPanel Apache.
  2. Remove existing edge bans. For each banned Cloudflare IP: csf -dr 172.70.10.10 (and check csf.tempban via csf -t). Symptoms clear immediately for the affected POPs.
  3. Make LFD unable to ban the edge again. Add Cloudflare’s ranges to /etc/csf/csf.ignore so LFD stops acting on log hits from them, with port-scoped entries in csf.allow as appropriate — the careful version, including keeping the list current, is in allowlisting Cloudflare IP ranges in CSF and LFD. One asymmetry to internalize: with mod_remoteip in place, ModSecurity attributes hits to the real client, but a CSF ban of that client’s IP is useless against proxied traffic — the TCP connections still come from Cloudflare, so iptables never sees the visitor’s address. Blocking abusive clients behind the proxy belongs at the edge (a WAF custom rule or IP Access rule in the Cloudflare dashboard), not in iptables.
  4. Divide the labour deliberately. Let Cloudflare handle volumetric noise, scrapers, and generic bot traffic at the edge; keep ModSecurity (CRS or your vendor set) as the application-aware backstop at the origin. With that split, consider disabling CRS’s pure rate/DoS rules at the origin — the edge does that job with better data.
  5. Handle genuine per-rule false positives normally. Read the rule ID from the Hits List and disable it for the affected domain only — WHM » Security Center » ModSecurity Tools » Rules List, or a scoped SecRuleRemoveById include — as covered in disabling a ModSecurity rule per account. Cloudflare does not cause rule 941100 to misfire on a page builder; that class of false positive predates the proxy and outlives it.

When this fix is wrong

If the hits are real attacks arriving through the proxy, none of this is suppression territory: block the actual client (now visible thanks to mod_remoteip) at the Cloudflare edge, and let ModSecurity keep flagging. If the 403s users report carry Cloudflare branding or a cf-mitigated header, the block came from the edge WAF, not ModSecurity — tune the Cloudflare side instead. And if the server does not run CSF/LFD, the enforcement half of this article does not apply, though the attribution half still does.

Common mistakes

Whitelisting Cloudflare ranges inside ModSecurity (an @ipMatch allow on REMOTE_ADDR) before installing mod_remoteip — since every proxied request comes from those ranges, that disables the WAF for all real traffic in one line. Disabling ModSecurity server-wide to stop one noisy rule. Deleting a rule globally because one customer’s plugin trips it. Leaving stale edge-IP bans in csf.deny after fixing the config, then blaming Cloudflare for lingering 522s.

Prevention

Provision new cPanel servers with mod_remoteip, the CSF ignore list, and the edge/origin WAF split as a package — the three fail as a package when any one is missing. Review the ModSecurity Hits List weekly, and when a new rule vendor or paranoia level is introduced, stage it in detection-only mode first so aggregation artifacts show up as log lines instead of firewalled edge IPs.

Running Cloudflare in front of cPanel/WHM is a stack Stack Harbor operates daily as an official cPanel NOC partner — WHM server management covers the DNS, mail, SSL, and firewall plumbing, and you can start a conversation any time.