Skip to content

Instantly share code, notes, and snippets.

@budiantoip
Created November 25, 2024 03:09
Show Gist options
  • Save budiantoip/ba22ffd86d9b94c2d981b24afd1d3b26 to your computer and use it in GitHub Desktop.
Save budiantoip/ba22ffd86d9b94c2d981b24afd1d3b26 to your computer and use it in GitHub Desktop.
Nginx snippets

Implement rate-limiting rule for non asset-file requests

# Define the rate limit zone
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;

server {
    # Exclude CSS, JS, and image files from rate limiting
    location ~* \.(css|js|jpg|jpeg|png|gif|webp|svg|ico)$ {
        # No rate limiting applied for these file types
        # You can put other configurations here if necessary
    }
    
    # Apply rate limiting to all requests
    location / {
        limit_req zone=one burst=10 nodelay;
    }
}

The above nginx config will process the first 10 requests from the same IP address for non-asset file requests, and will continue procesing the next 10 requests right away, and reject the 21th request and afterwards. This is useful to prevent DDoS attack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment