While network firewalls like UFW block unauthorized ports, and tools like Fail2Ban monitor logs for brute-force attempts, neither is designed to handle a massive, sudden influx of legitimate-looking HTTP traffic. An Application-Layer (Layer 7) DDoS attack, often called an HTTP flood, attempts to exhaust your server’s CPU and memory by overwhelming your application with thousands of rapid requests to resource-heavy pages.

If you are running a VPS or dedicated server, implementing rate limiting directly at your web server level (such as Nginx or Apache) is a highly effective, low-overhead way to throttle aggressive traffic, protect your backend databases from crashing, and keep your site online during a targeted flood.

Understanding Web Server Rate Limiting

Rate limiting works by tracking the IP addresses of incoming requests and restricting how many requests a single IP can make within a specified timeframe.

When a botnet or aggressive scraper exceeds this predefined limit, the web server immediately intercepts the request and returns an HTTP 503 (Service Unavailable) or 429 (Too Many Requests) error. By dropping the connection at the web server layer, you prevent the heavy application code (like Node.js, Python, or PHP) and the database from ever having to process the malicious request.

Configuring Rate Limiting in Nginx

Nginx is uniquely equipped to handle rate limiting efficiently using the "leaky bucket" algorithm. Setup requires defining a shared memory zone to track the IP addresses, and then applying that zone to your server blocks.

  • Connect to your server via SSH and open your main Nginx configuration file: sudo nano /etc/nginx/nginx.conf.

  • Inside the http { ... } block, define your tracking zone by adding: limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;

  • $binary_remote_addr: Tracks the client IP address in a compressed binary format to save RAM.

  • zone=mylimit:10m: Allocates 10 megabytes of memory to this zone (enough to track roughly 160,000 IP addresses simultaneously).

  • rate=5r/s: Sets the strict limit to 5 requests per second per IP.

Applying the Limits to Vulnerable Endpoints

You generally do not want to heavily rate-limit static assets like images or CSS files, as a normal browser loads dozens of these simultaneously. Instead, you should apply the limit to dynamic endpoints, such as login pages, search forms, or API routes.

  • Open the configuration file for your specific website (e.g., sudo nano /etc/nginx/sites-available/mywebsite).

  • Locate the location block for your dynamic application or API.

  • Apply the zone you created by adding the limit_req directive:

    Plaintext
     
    location /api/ {
        limit_req zone=mylimit;
        proxy_pass http://localhost:3000;
    }
    
  • Save the file and verify your syntax to ensure you haven't broken the configuration: sudo nginx -t.

  • If the test passes, reload the web server to activate the defenses: sudo systemctl reload nginx.

Handling Legitimate Traffic Spikes (Burst)

A strict limit of 5 requests per second might accidentally block legitimate users if their browser requests a few API endpoints simultaneously. Nginx allows you to configure a "burst" queue to handle sudden, normal traffic spikes gracefully.

  • Modify your location block directive to include the burst parameter: limit_req zone=mylimit burst=10 nodelay;

  • burst=10: Allows a user to instantly exceed the 5 requests/second limit by up to 10 additional requests, queuing them in memory instead of immediately dropping them.

  • nodelay: Ensures that those burst requests are processed instantly rather than being artificially slowed down to match the 5r/s drip rate, keeping your application feeling fast for legitimate users while still punishing aggressive bots.

Ha estat útil la resposta? 0 Els usuaris han Trobat Això Útil (0 Vots)