ratelimit
A Caddy v2 extension to apply rate-limiting for HTTP requests.
Installation
$ xcaddy build --with github.com/owlwang/caddy-ratelimit
Caddyfile Syntax
rate_limit [
]
[
[
]]
Parameters:
{path.}
{query.}
{header.}
{cookie.}
{body.}
(requires the requestbodyvar extension){remote.host}
(ignores theX-Forwarded-For
header){remote.port}
{remote.ip}
(prefers the first IP in theX-Forwarded-For
header){remote.host_prefix.
(CIDR block version of} {remote.host}
){remote.ip_prefix.
(CIDR block version of} {remote.ip}
)
Example
With the following Caddyfile:
localhost:8080 {
route /foo {
rate_limit {query.id} 2r/m
respond 200
}
}
You can apply the rate of 2 requests per minute
to the path /foo
, and Caddy will respond with status code 429 when a client exceeds:
$ curl -w "%{http_code}" 'https://localhost:8080/foo?id=1'
200
$ curl -w "%{http_code}" 'https://localhost:8080/foo?id=1'
200
$ curl -w "%{http_code}" 'https://localhost:8080/foo?id=1'
429
An extra request with other value for the request parameter id
will not be limited:
$ curl -w "%{http_code}" 'https://localhost:8080/foo?id=2'
200