Rule Gallery
Explore a curated collection of example configurations spanning from common to unconventional use cases for the Traffic Policy module.
A number of these examples come from a longer article about how ngrok makes policy management accessible to developers, including a simple Go-based application for testing these and other configurations.
See the following categories for specific expressions and actions:
Authentication
Add JWT authentication and key-based rate limiting
Building from our Auth0 guide, these rules also add rate limiting based on your consumers' JWTs.
- YAML
- JSON
# snippet
---
on_http_request:
- expressions: []
name: Add JWT authentication and rate limiting
actions:
- type: rate-limit
config:
name: Only allow 30 requests per minute
algorithm: sliding_window
capacity: 30
rate: 60s
bucket_key:
- req.headers['x-api-key']
- type: jwt-validation
config:
issuer:
allow_list:
- value: https://<YOUR-AUTH-PROVIDER>
audience:
allow_list:
- value: <YOUR-NGROK-DOMAIN>
http:
tokens:
- type: jwt
method: header
name: Authorization
prefix: "Bearer "
jws:
allowed_algorithms:
- RS256
keys:
sources:
additional_jkus:
- https://<YOUR-AUTH-PROVIDER>/.well-known/jwks.json
on_http_response: []
// snippet
{
"on_http_request": [
{
"expressions": [],
"name": "Add JWT authentication and rate limiting",
"actions": [
{
"type": "rate-limit",
"config": {
"name": "Only allow 30 requests per minute",
"algorithm": "sliding_window",
"capacity": 30,
"rate": "60s",
"bucket_key": [
"req.headers['x-api-key']"
]
}
},
{
"type": "jwt-validation",
"config": {
"issuer": {
"allow_list": [
{
"value": "https://<YOUR-AUTH-PROVIDER>"
}
]
},
"audience": {
"allow_list": [
{
"value": "<YOUR-NGROK-DOMAIN>"
}
]
},
"http": {
"tokens": [
{
"type": "jwt",
"method": "header",
"name": "Authorization",
"prefix": "Bearer "
}
]
},
"jws": {
"allowed_algorithms": [
"RS256"
],
"keys": {
"sources": {
"additional_jkus": [
"https://<YOUR-AUTH-PROVIDER>/.well-known/jwks.json"
]
}
}
}
}
}
]
}
],
"on_http_response": []
}
Rate limiting
Rate limit for specific endpoint
This rule applies rate limiting of 30
requests per second to the endpoint
/api/videos
.
- YAML
- JSON
# snippet
---
on_http_request:
- expressions:
- req.url.contains('/api/specific_endpoint')
actions:
- type: rate-limit
config:
name: Only allow 30 requests per minute
algorithm: sliding_window
capacity: 30
rate: 60s
bucket_key:
- conn.client_ip
// snippet
{
"on_http_request": [
{
"expressions": [
"req.url.contains('/api/specific_endpoint')"
],
"actions": [
{
"type": "rate-limit",
"config": {
"name": "Only allow 30 requests per minute",
"algorithm": "sliding_window",
"capacity": 30,
"rate": "60s",
"bucket_key": [
"conn.client_ip"
]
}
}
]
}
]
}
Rate limit API consumers based on authentication status
Create a low rate limit for unauthenticated (likely free) users, while allowing authenticated users a higher level of capacity.
- YAML
- JSON
# snippet
---
on_http_request:
- expressions:
- "!('Authorization' in req.headers)"
name: Unauthorized rate limiting tier
actions:
- type: rate-limit
config:
name: Allow 10 requests per minute
algorithm: sliding_window
capacity: 10
rate: 60s
bucket_key:
- conn.client_ip
- expressions:
- ('Authorization' in req.headers)
name: Authorized rate limiting tier
actions:
- type: rate-limit
config:
name: Allow 100 requests per minute
algorithm: sliding_window
capacity: 100
rate: 60s
bucket_key:
- conn.client_ip
on_http_response: []
// snippet
{
"on_http_request": [
{
"expressions": [
"!('Authorization' in req.headers)"
],
"name": "Unauthorized rate limiting tier",
"actions": [
{
"type": "rate-limit",
"config": {
"name": "Allow 10 requests per minute",
"algorithm": "sliding_window",
"capacity": 10,
"rate": "60s",
"bucket_key": [
"conn.client_ip"
]
}
}
]
},
{
"expressions": [
"('Authorization' in req.headers)"
],
"name": "Authorized rate limiting tier",
"actions": [
{
"type": "rate-limit",
"config": {
"name": "Allow 100 requests per minute",
"algorithm": "sliding_window",
"capacity": 100,
"rate": "60s",
"bucket_key": [
"conn.client_ip"
]
}
}
]
}
],
"on_http_response": []
}
Rate limit API consumers based on pricing tiers
Using a naming scheme in your upstream servers, and API calls using a tier
header, you can quickly customize access to your API based on any number of pricing tiers.
- YAML
- JSON
# snippet
---
on_http_request:
- expressions:
- "!('Tier' in req.headers)"
name: Free rate limiting tier
actions:
- type: rate-limit
config:
name: Allow 10 requests per minute
algorithm: sliding_window
capacity: 10
rate: 60s
bucket_key:
- conn.client_ip
- expressions:
- getReqHeader('tier').exists(v, v.matches('(?i)bronze'))
name: Bronze rate limiting tier
actions:
- type: rate-limit
config:
name: Allow 100 requests per minute
algorithm: sliding_window
capacity: 100
rate: 60s
bucket_key:
- conn.client_ip
- expressions:
- getReqHeader('tier').exists(v, v.matches('(?i)silver'))
name: Bronze rate limiting tier
actions:
- type: rate-limit
config:
name: Allow 1000 requests per minute
algorithm: sliding_window
capacity: 1000
rate: 60s
bucket_key:
- conn.client_ip
- expressions:
- getReqHeader('tier').exists(v, v.matches('(?i)gold'))
name: Gold rate limiting tier
actions:
- type: rate-limit
config:
name: Allow 10000 requests per minute
algorithm: sliding_window
capacity: 10000
rate: 60s
bucket_key:
- conn.client_ip
on_http_response: []
// snippet
{
"on_http_request": [
{
"expressions": [
"!('Tier' in req.headers)"
],
"name": "Free rate limiting tier",
"actions": [
{
"type": "rate-limit",
"config": {
"name": "Allow 10 requests per minute",
"algorithm": "sliding_window",
"capacity": 10,
"rate": "60s",
"bucket_key": [
"conn.client_ip"
]
}
}
]
},
{
"expressions": [
"getReqHeader('tier').exists(v, v.matches('(?i)bronze'))"
],
"name": "Bronze rate limiting tier",
"actions": [
{
"type": "rate-limit",
"config": {
"name": "Allow 100 requests per minute",
"algorithm": "sliding_window",
"capacity": 100,
"rate": "60s",
"bucket_key": [
"conn.client_ip"
]
}
}
]
},
{
"expressions": [
"getReqHeader('tier').exists(v, v.matches('(?i)silver'))"
],
"name": "Bronze rate limiting tier",
"actions": [
{
"type": "rate-limit",
"config": {
"name": "Allow 1000 requests per minute",
"algorithm": "sliding_window",
"capacity": 1000,
"rate": "60s",
"bucket_key": [
"conn.client_ip"
]
}
}
]
},
{
"expressions": [
"getReqHeader('tier').exists(v, v.matches('(?i)gold'))"
],
"name": "Gold rate limiting tier",
"actions": [
{
"type": "rate-limit",
"config": {
"name": "Allow 10000 requests per minute",
"algorithm": "sliding_window",
"capacity": 10000,
"rate": "60s",
"bucket_key": [
"conn.client_ip"
]
}
}
]
}
],
"on_http_response": []
}