0
0
Nginxdevops~5 mins

Why rate limiting prevents abuse in Nginx - Why It Works

Choose your learning style9 modes available
Introduction
Rate limiting helps stop too many requests from one user or device in a short time. This protects websites from being overwhelmed or attacked by too many requests at once.
When a website gets too many requests from one IP and slows down for others
When you want to stop automated bots from spamming your login page
When you want to protect an API from being called too often by one user
When you want to avoid extra costs caused by excessive traffic
When you want to keep your server stable during traffic spikes
Config File - nginx.conf
nginx.conf
http {
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;

    server {
        listen 80;

        location / {
            limit_req zone=mylimit burst=10 nodelay;
            proxy_pass http://localhost:8080;
        }
    }
}

limit_req_zone defines a shared memory zone named 'mylimit' that tracks requests per IP address, allowing 5 requests per second.

limit_req applies the rate limit to the location, allowing bursts of up to 10 requests without delay.

This setup protects the server by slowing down or rejecting excessive requests from the same IP.

Commands
Check the Nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to apply the new rate limiting configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to the server to check if it responds normally under the rate limit.
Terminal
curl -i http://localhost/
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Content-Type: text/html Content-Length: 612 Connection: keep-alive <html>...</html>
Send 20 quick requests to test if rate limiting blocks some requests with status 503.
Terminal
for i in {1..20}; do curl -s -o /dev/null -w "%{http_code}\n" http://localhost/; done
Expected OutputExpected
200 200 200 200 200 503 503 503 503 503 503 503 503 503 503 503 503 503 503 503
Key Concept

If you remember nothing else from this pattern, remember: rate limiting controls how many requests a user can make to protect your server from overload and abuse.

Common Mistakes
Not testing the Nginx configuration before reloading
This can cause Nginx to fail to reload and stop serving requests if there is a syntax error.
Always run 'sudo nginx -t' to check configuration syntax before reloading.
Setting the rate limit too low without allowing bursts
This can block normal users who occasionally make multiple requests quickly.
Use the 'burst' parameter to allow short bursts of requests without blocking.
Not reloading Nginx after changing the config file
Changes won't take effect until Nginx reloads the configuration.
Run 'sudo systemctl reload nginx' after editing the config.
Summary
Define a rate limit zone in nginx.conf to track requests per IP.
Apply the rate limit to a location with 'limit_req' and configure burst handling.
Test the configuration syntax with 'nginx -t' before reloading.
Reload Nginx to apply changes without downtime.
Verify rate limiting works by sending multiple requests and checking for 503 errors.