keepcalculator

06 Numbers and Powers

Fail2ban Stop Apache DDoS? Real Limits

Card No. 06·68419 · Updated · Standard formula, shown below

Fail2ban can stop low-and-slow application-layer DDoS attacks against Apache by banning the attacking IPs, but it is not a full DDoS solution and will not protect against large-scale distributed attacks or high-bandwidth floods. In short: it helps against targeted, repetitive abuse from a limited number of IPs, but a real DDoS needs a dedicated mitigation service.

What Fail2ban Actually Does

Fail2ban is a log-scanning daemon. It watches Apache access and error logs, applies regular-expression filters to detect patterns (like repeated 404s, login failures, or too many requests), and then updates the system firewall (iptables, nftables, or firewalld) to drop traffic from the offending IP for a set time. It is a reactive tool: it only acts after the attack pattern appears in the logs.

For Apache specifically, you can define a jail that counts requests per IP over a short window. For example, a filter that matches every HTTP request line allows you to ban an IP that makes more than 300 requests in 60 seconds. This stops a single machine or a small botnet from hammering your server with GET requests.

Where It Fails Against DDoS

The core weakness is that DDoS attacks are distributed. A botnet of 10,000 unique IPs, each sending only a few requests per second, will not trigger Fail2ban’s per-IP threshold. Each IP stays below the ban limit, and the aggregate traffic overwhelms the server or the network link. Fail2ban also does nothing against:

  • Volumetric floods — UDP floods, ICMP floods, or SYN floods that saturate bandwidth before Apache even sees the traffic.
  • Slowloris and slow-body attacks — these hold connections open without completing requests, so no complete log line is written until the connection times out.
  • Application-layer attacks that rotate IPs — attackers using cloud instances or proxy lists never send enough from one IP to trip the ban.

How to Configure Fail2ban for Apache

To make Fail2ban as effective as possible against application-layer abuse, you need a custom jail. Here is a practical setup:

  1. Create a filter file /etc/fail2ban/filter.d/apache-dos.conf that matches every request line in the access log:
# apache-dos.conf
[Definition]
failregex = ^<HOST> -.*"(GET|POST|HEAD)
ignoreregex =
  1. Create a jail entry in /etc/fail2ban/jail.local:
[apache-dos]
enabled = true
port = http,https
filter = apache-dos
logpath = /var/log/apache2/access.log
maxretry = 300
findtime = 60
bantime = 600

This bans an IP that makes 300 requests in 60 seconds for 10 minutes. Adjust the numbers based on your normal traffic — a busy site might need higher thresholds to avoid banning legitimate visitors.

Common Mistakes and Misconceptions

Mistake 1: Banning too aggressively. If you set maxretry too low (like 20 requests per minute), you will ban real users behind NAT or corporate proxies. Many offices share one public IP, and a single user refreshing a page can trip the ban.

Mistake 2: Ignoring the log format. Apache logs can be in combined or common format, and the regex must match your actual log lines. Test the filter with fail2ban-regex /var/log/apache2/access.log /etc/fail2ban/filter.d/apache-dos.conf before enabling it.

Mistake 3: Thinking Fail2ban stops SYN floods. It does not. SYN floods happen at the TCP handshake level and never generate Apache access log entries. You need kernel-level tuning (like net.ipv4.tcp_syncookies) or a firewall with SYN proxy.

Misconception: Fail2ban is useless. That is not true. It is excellent at stopping credential stuffing, comment spam, and targeted scraping from a fixed set of IPs. It is just not a DDoS panacea.

Alternatives and Complements

For real DDoS protection, you need a layer that filters traffic before it reaches your server:

  • CDN or DDoS mitigation service — providers like Cloudflare or AWS Shield absorb volumetric attacks and filter malicious requests at the edge.
  • ModSecurity (WAF) — a web application firewall that can block request patterns (like SQLi or XSS) before they hit Apache.
  • Apache modules — mod_evasive or mod_security can throttle requests per IP at the application level, but they have the same per-IP limitation.
  • Kernel and network tuning — increase the SYN backlog, enable syncookies, and use rate limiting in iptables/nftables for connection floods.

For a small site behind a decent CDN, Fail2ban is a useful second layer. For a large site under sustained attack, it is not enough on its own.

Cost and Maintenance

Fail2ban is free and open-source, and the maintenance cost is low — you mainly need to keep the regex filters up to date with your Apache log format. The real cost is the risk of false positives: a misconfigured jail can block legitimate users, costing you traffic and trust. Test every filter on a staging server or with fail2ban-regex before deploying.

Frequently Asked Questions

Can Fail2ban stop a Layer 7 DDoS?

Only if the attack comes from a small number of IPs that exceed your request threshold. If the attack is distributed across thousands of IPs, each staying under the limit, Fail2ban will not help — you need a WAF or CDN that can analyze request patterns globally.

What is a good maxretry and findtime for Apache?

For a typical small-to-medium site, start with maxretry=300 and findtime=60 (300 requests per minute). Monitor your access logs for a week to see the peak legitimate request rate per IP, then set the threshold 2-3 times higher to avoid false bans.

Does Fail2ban protect against slowloris?

Not by default. Slowloris sends partial requests and never completes them, so no full log line is written. You can enable the mod_reqtimeout module in Apache to close slow connections, or use Fail2ban with a filter that matches “connection timeout” error log entries, but it is not the primary defense.

Will Fail2ban slow down my server?

No, Fail2ban itself is lightweight — it tails log files and runs regex checks. The main performance cost is the firewall rules it adds, which are negligible for a few thousand banned IPs. The bigger risk is banning a shared IP and losing legitimate traffic.

Can I use Fail2ban with a CDN?

Yes, but you must configure it to read the real client IP from the X-Forwarded-For header, and you should only ban IPs that the CDN does not already filter. Many CDNs already block abusive IPs, so Fail2ban may rarely trigger — but it is a useful fallback for direct-to-origin attacks.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →