How to Configure Fail2Ban with ModSecurity on CloudPanel (Nginx)

Nemesis

Active member
Joined
Mar 22, 2025
Messages
35
Points
61
Age
26
I want to integrate Fail2Ban with ModSecurity on my CloudPanel server (Nginx-based) to automatically block IPs flagged by ModSecurity rules. How can I properly configure this, and what are the challenges?
 
Solution
CloudPanel does not come with ModSecurity or Fail2Ban pre-installed, but you can configure both manually. This process requires advanced server management skills since CloudPanel uses Nginx, and ModSecurity is not natively integrated like it is in Apache environments (e.g., Plesk).

🔸 Step-by-Step: ModSecurity + Fail2Ban Setup for CloudPanel​

1. Install ModSecurity v3 for Nginx


Follow the steps to install ModSecurity v3 (libmodsecurity) with Nginx. [See our earlier guide here] or search for official ModSecurity v3 installation steps.

Make sure it’s logging to a dedicated file, e.g.:

Code:
SecAuditEngine On
SecAuditLogRelevantStatus "403,404,500"
SecAuditLog /var/log/modsec_audit.log

2. Install...

CloudPanel does not come with ModSecurity or Fail2Ban pre-installed, but you can configure both manually. This process requires advanced server management skills since CloudPanel uses Nginx, and ModSecurity is not natively integrated like it is in Apache environments (e.g., Plesk).

🔸 Step-by-Step: ModSecurity + Fail2Ban Setup for CloudPanel​

1. Install ModSecurity v3 for Nginx


Follow the steps to install ModSecurity v3 (libmodsecurity) with Nginx. [See our earlier guide here] or search for official ModSecurity v3 installation steps.

Make sure it’s logging to a dedicated file, e.g.:

Code:
SecAuditEngine On
SecAuditLogRelevantStatus "403,404,500"
SecAuditLog /var/log/modsec_audit.log

2. Install Fail2Ban

Code:
sudo apt install fail2ban -y

3. Create Fail2Ban Filter for ModSecurity


Create the filter file:

Code:
sudo nano /etc/fail2ban/filter.d/modsecurity.conf

Paste this example filter:

[Definition]
Code:
failregex = \[.*?\] \[.*?\] \[client <HOST>\] ModSecurity: .*Matched "Operator .*"


🔎 You can customize the failregex based on the ModSecurity version and log format you're using.

4. Create a Jail File


Create a jail configuration for ModSecurity:

Code:
sudo nano /etc/fail2ban/jail.d/modsecurity.conf

Add this:
Code:
[modsecurity]
enabled  = true
filter   = modsecurity
action   = iptables-multiport[name="modsecurity", port="http,https"]
logpath  = /var/log/modsec_audit.log
maxretry = 3
findtime = 600
bantime  = 3600

5. Restart Fail2Ban

Code:
sudo systemctl restart fail2ban

Check status:

Code:
sudo fail2ban-client status modsecurity

Known Issues (Based on Real-World Feedback):​

Users running ModSecurity + Fail2Ban on NGINX (not Apache) have reported issues such as:
  • ModSecurity stops logging after a while.
  • Fail2Ban doesn’t detect IPs due to incorrect or missing failregex.
  • Nginx stability issues after enabling ModSecurity, especially on VPS systems.

🔧 Suggested Workarounds:​

  • Use Cloudflare WAF or Sucuri Firewall for WAF-level protection.
  • If using ModSecurity, consider writing a cron script that parses logs and adds IPs to a ban list manually (if Fail2Ban proves unstable).

Summary:​


FeatureSupported in CloudPanel?Manual Setup Required
ModSecurity❌ Not built-in✅ Yes
Fail2Ban Integration❌ Not preconfigured✅ Yes (via jail/filter setup)
 
Solution
Back
Top