Cloudflare Security
Custom Rules Rate Limiting IP Access Expressions

Rate Limiting

Rate Limiting controls the number of requests allowed within a time period, protecting against brute force attacks and API abuse.


Protect Login

Limit login attempts

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:


Progressive rate limit

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")

Protect APIs

General API limit

Expression:

(http.request.uri.path starts_with "/api/")

Configuration:


Limit per specific endpoint

Expression:

(http.request.uri.path eq "/api/expensive-operation")

Configuration:


Limit for authenticated vs anonymous users

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 "")

Protect Against Credential Stuffing

Detect attack pattern

Expression:

(http.request.uri.path eq "/login") and (http.request.method eq "POST")

Configuration:

Logic: No human types credentials 3 times in 10 seconds.


Protect Forms

Limit form submissions

Expression:

(http.request.uri.path eq "/contact" or http.request.uri.path eq "/newsletter") and (http.request.method eq "POST")

Configuration:


Counting Characteristics

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

Custom Response

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)


Monitoring

After configuring rate limiting:

  1. Go to Security > Events in the dashboard
  2. Filter by “Rate Limiting”
  3. Analyze blocked traffic patterns
  4. Adjust thresholds as needed

Tips


Back to Cloudflare View Snippets