Rate Limiting controls the number of requests allowed within a time period, protecting against brute force attacks and API abuse.
Expression:
(http.request.uri.path eq "/api/auth/login" or http.request.uri.path eq "/login") and (http.request.method eq "POST")
Configuration:
Why it works:
Create multiple rules with increasing penalties:
Rule 1 - Warning (Log)
(http.request.uri.path eq "/login") and (http.request.method eq "POST")
Rule 2 - Challenge
(http.request.uri.path eq "/login") and (http.request.method eq "POST")
Rule 3 - Block
(http.request.uri.path eq "/login") and (http.request.method eq "POST")
Expression:
(http.request.uri.path starts_with "/api/")
Configuration:
Expression:
(http.request.uri.path eq "/api/expensive-operation")
Configuration:
Rule 1 - Anonymous (more restrictive)
(http.request.uri.path starts_with "/api/") and not (http.request.headers["authorization"][0] ne "")
Rule 2 - Authenticated (more permissive)
(http.request.uri.path starts_with "/api/") and (http.request.headers["authorization"][0] ne "")
Expression:
(http.request.uri.path eq "/login") and (http.request.method eq "POST")
Configuration:
Logic: No human types credentials 3 times in 10 seconds.
Expression:
(http.request.uri.path eq "/contact" or http.request.uri.path eq "/newsletter") and (http.request.method eq "POST")
Configuration:
Cloudflare allows counting requests by different criteria:
| Characteristic | Use |
|---|---|
| IP | Default, blocks by IP address |
| IP + URI Path | Different limit per endpoint |
| Headers | Useful for APIs with API keys |
| Cookie | Tracking by session |
| Query String | Limit per parameter |
You can configure a custom response when rate limit is reached:
{
"error": "rate_limit_exceeded",
"message": "Too many requests. Please try again in a few minutes.",
"retry_after": 60
}
Response Code: 429 (Too Many Requests)
After configuring rate limiting:
| Back to Cloudflare | View Snippets |