To set up Fail2ban to protect your Nginx server from a DDoS attack targeting your Bitrix24 CRM, you will need to create a custom jail and filter that specifically targets excessive requests from individual IP addresses. Here are the steps you can follow to accomplish this:
Step 1: Install Fail2ban
If Fail2ban is not already installed, you can install it using your package manager. For example, on a Debian-based system, you can use:
sudo apt update
sudo apt install fail2ban
Step 2: Configure Fail2ban
2.1 Create Nginx Jail Config
Open or create the jail configuration file for Nginx. This file can be located at /etc/fail2ban/jail.local or a specific file under /etc/fail2ban/jail.d/.
sudo nano /etc/fail2ban/jail.d/nginx-security.conf
Add the following configuration for Nginx to block IPs with too many requests:
[nginx-http-repeated-request]
enabled = true
filter = nginx-http-repeated-request
action = iptables[name=HTTP, port=http, protocol=tcp]
logpath = /var/log/nginx/access.log
maxretry = 100
findtime = 1m
bantime = 24h
- maxretry: The maximum number of requests before the IP is banned.
- findtime: The time window within which the
maxretry number of requests must occur for an IP to get banned.
- bantime: The duration for which the IP will be banned.
Adjust these values based on your server load and traffic pattern.
2.2 Create Custom Filter
Create a custom filter that matches the pattern of excessive HTTP requests. Open the filter file:
sudo nano /etc/fail2ban/filter.d/nginx-http-repeated-request.conf
Add the following content to match typical nginx access log requests:
[Definition]
failregex = ^<HOST> -.*"(GET|POST).*
ignoreregex =
This filter targets repeated GET or POST requests. You can adapt the failregex based on the actual entries found in your Nginx access log.
Step 3: Test the Configuration
Test your regex to ensure it works as expected:
fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-http-repeated-request.conf
Ensure the results show the expected IP addresses being matched according to your log entries.
Step 4: Restart Fail2ban
Once you have finished your configuration, restart the Fail2ban service to apply the changes:
sudo systemctl restart fail2ban
Verify Fail2ban is running and your jail is active:
sudo fail2ban-client status
The output should show your custom jail nginx-http-repeated-request as active, along with any banned IPs under its status.
Additional Considerations
- Whitelist Trusted IPs: Make sure to whitelist any IPs that should never be banned (e.g., internal IPs or known partners).
- Regularly Monitor: Monitor your Fail2ban logs and banned IPs to make adjustments if you're experiencing too many false positives.
- Fine-Tune Settings: Depending on the traffic patterns and load typical to your Bitrix24 usage, you may have to adjust
maxretry, findtime, and bantime values to avoid disrupting normal user access.
Fail2ban is a useful tool for mitigating small-scale DDoS attacks and automated abusive traffic but may not be effective against large-scale attacks. In such situations, consider additional DDoS protection services and solutions.