Shielding Your Nginx Server from DDoS and Brute Force Attacks: Our Experience at GyanAangan

Shielding Your Nginx Server from DDoS and Brute Force Attacks: Our Experience at GyanAangan

By Devang Shaurya Pratap Singh Singhcloud
Advertisement

Secure Your Nginx Server
At GyanAangan, safeguarding our online platforms is our top priority. In response to a recent wave of brute-force and DDoS attempts on our Nginx server, we strengthened our defenses by implementing tools and settings like Fail2Ban and Nginx Rate Limiting. Here’s a breakdown of how these configurations work, why they’re essential, and how you can set them up to protect your own website.


Understanding the Threats: Brute Force and DDoS Attacks

Brute-force and DDoS (Distributed Denial of Service) attacks are two of the most common methods cybercriminals use to overwhelm a server or hack into a system.

  • Brute-force attacks involve attackers repeatedly attempting to guess passwords or other login credentials. Left unchecked, they can eventually break through security, compromising data and server integrity.
  • DDoS attacks flood a server with an overwhelming number of requests, causing downtime, reduced performance, or even server crashes, which impacts your website’s availability.

We turned to Fail2Ban and Nginx Rate Limiting as robust countermeasures. Here’s how they work and how you can set them up.


Step 1: Implementing Fail2Ban to Combat Brute Force Attacks

Fail2Ban is a security tool that monitors server logs for malicious behavior, such as repeated failed login attempts. Once it identifies a potential attack, it temporarily bans the offending IP address, effectively thwarting brute-force attempts.

Installing and Configuring Fail2Ban

  1. Install Fail2Ban on your server:

    sudo apt-get install fail2ban
    
    	
  2. Configure Fail2Ban for Nginx: Fail2Ban operates based on rules set in configuration files. Start by creating or modifying the jail configuration file:

    sudo nano /etc/fail2ban/jail.local
    
    	
  3. Define the Nginx rules: Add these configurations to detect and block malicious IPs targeting Nginx:

    [nginx-http-auth]
    enabled = true
    port = http,https
    filter = nginx-http-auth
    logpath = /var/log/nginx/error.log
    maxretry = 3
    bantime = 600
    • maxretry: Limits the number of allowed failed attempts (e.g., 3 tries).
    • bantime: Specifies the time (in seconds) an IP should be banned (e.g., 600 seconds).
       
  4. Restart Fail2Ban to apply the configuration

    sudo systemctl restart fail2ban
    
    	

With this configuration, Fail2Ban will actively monitor Nginx logs, and when it detects multiple failed login attempts from a single IP, it bans that IP, blocking the attacker.


Step 2: Setting Up Nginx Rate Limiting to Deter DDoS Attacks

Rate limiting controls the number of requests a client can make to your server within a specific timeframe. This is particularly effective in preventing DDoS attacks by preventing a single client from overwhelming the server.

Configuring Nginx Rate Limiting

  1. Open the Nginx Configuration File:

    sudo nano /etc/nginx/nginx.conf
    
    
  2. Define a Rate-Limiting Zone: Add a limit to the http block in nginx.conf:

    http {
        limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
    }
    • $binary_remote_addr: Tracks client IPs.
    • zone=mylimit:10m: Defines a 10MB shared memory zone for rate-limiting.
    • rate=10r/s: Limits requests to 10 per second for each IP.
       
  3. Apply the Rate Limit to a Location Block: Within your server configuration, specify where the rate limit should apply (e.g., all pages or specific paths):

    server { 
        location /{
            limit_req zone=mylimit burst=20 nodelay; 
        }
    } 
    • burst=20: Allows a short burst of up to 20 requests.
    • nodelay: Ensures requests are processed instantly up to the burst limit.
       
  4. Restart Nginx to activate the changes:

    sudo systemctl restart nginx
    
    

Combining Fail2Ban and Nginx Rate Limiting for Maximum Protection

Both Fail2Ban and Nginx Rate Limiting offer powerful defenses individually, but when used together, they create a comprehensive protection system for your Nginx server.

Fail2Ban guards against sustained login attempts, while Rate Limiting prevents excessive requests from overwhelming your server. This dual-layered approach has proven effective for us at GyanAangan, and it can significantly strengthen your website’s security against brute-force and DDoS attacks.


Wrapping Up

Implementing these measures requires a bit of configuration but goes a long way toward protecting your online presence. At GyanAangan, these strategies have helped us create a more resilient and secure environment for our users. By following these steps, you, too, can safeguard your server against potential threats.

Advertisement
2025 GyanAangan.in All rights reserved.