How to Configure Fail2ban to Protect Nginx Server on Ubuntu 24.04 LTS
Securing a web server is one of the most critical steps after deploying any website or web application. If you are running an Nginx web server on Ubuntu 24.04 LTS, it is constantly exposed to automated botnets, brute-force attacks, and malicious vulnerability scanners.
While configuring a UFW Firewall on Ubuntu 24.04 LTS blocks unauthorized ports, Fail2ban provides dynamic security by analyzing server log files in real-time and banning IP addresses that show malicious behavior. Furthermore, when combined with proper SSH Security using Fail2ban, your entire server architecture becomes resilient against external threats.
In this comprehensive guide, we will step-by-step configure Fail2ban to protect your Nginx web server against brute-force logins, spam bots, and DOS attempts.
Prerequisites
Before proceeding, ensure you have:
An Ubuntu 24.04 LTS server running with root or sudo privileges.
Nginx installed and actively serving content.
UFW (Uncomplicated Firewall) configured and enabled. If you haven't configured it yet, check out our guide on How to Set Up UFW Firewall on Ubuntu 24.04 LTS.
Step 1: Install Fail2ban on Ubuntu 24.04
Fail2ban is included in the default Ubuntu 24.04 repositories. Update your system package index and install Fail2ban using the following commands:sudo apt update
sudo apt install fail2ban -y
Once the installation is complete, verify that the Fail2ban service is active and running:sudo systemctl status fail2ban
Step 2: Create a Local Jail Configuration
Fail2ban uses configuration files stored in /etc/fail2ban/. The main configuration file is jail.conf, but you should never edit jail.conf directly` because system updates will overwrite it.
Instead, create a custom configuration file named jail.local:sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Now open the newly created file using your preferred text editor:sudo nano /etc/fail2ban/jail.local
Step 3: Configure Default Settings
Inside the [DEFAULT] section of /etc/fail2ban/jail.local, set the general rules that apply to all enabled services:[DEFAULT]
ignoreip = 127.0.0.1/8 ::1
bantime = 1h
findtime = 10m
maxretry = 5
banaction = ufw
ignoreip: Prevents your local machine or trusted IP addresses from being banned.
bantime: The duration an IP remains banned (e.g., 1h for 1 hour).
findtime: The timeframe during which repeated failed attempts are counted.
maxretry: The maximum number of failed attempts allowed before a ban is triggered.
banaction: Configures Fail2ban to use UFW to execute IP bans.
Step 4: Enable Nginx Protection Jails
Scroll down in /etc/fail2ban/jail.local or add specific jail configurations for Nginx at the bottom of the file:
1. Protect Against Nginx HTTP Auth Brute Force
If your website uses password-protected directories (htpasswd), enable the nginx-http-auth jail:[nginx-http-auth]
enabled = true
port = http,https
logpath = %(nginx_error_log)s
maxretry = 3
2. Block Nginx Bad Requests and Scanners
To block bots searching for vulnerable scripts or non-existent administrative panels (like /wp-login.php or phpmyadmin), enable nginx-botsearch:[nginx-botsearch]
enabled = true
port = http,https
logpath = %(nginx_error_log)s
maxretry = 2
findtime = 10m
bantime = 24h
Save and exit the text editor (CTRL + O, Enter, then CTRL + X).
Step 5: Restart and Verify Fail2ban
Apply the new rules by restarting the Fail2ban service:sudo systemctl restart fail2ban
To verify which jails are currently active, run:sudo fail2ban-client status
You should see output confirming that nginx-http-auth and nginx-botsearch are active.
Step 6: Monitor and Manage Banned IPs
To check the detailed status and view banned IP addresses for a specific jail (for example, nginx-botsearch), use:sudo fail2ban-client status nginx-botsearch
If you accidentally ban your own IP address, you can manually unban it using the following command:sudo fail2ban-client set nginx-botsearch unbanip YOUR_IP_ADDRESS
Conclusion
By pairing Nginx with Fail2ban on Ubuntu 24.04 LTS, you have added an automated defense layer that actively blocks malicious actors before they can consume your server resources.
To ensure complete server hardening, make sure to read our earlier Linux security tutorials:
How to Set Up UFW Firewall on Ubuntu 24.04 LTS🔗https://mobilestuff2026.blogspot.com/2026/10/how-to-set-up-ufw-firewall-on-ubuntu.html
How to Secure SSH with Fail2ban on Ubuntu 24.04 LTS
Comments
Post a Comment