Skip to content

rate_limit ​

rate_limit limits request throughput with a sliding window per key.

yaml
server:
  http:
    middlewares:
      limit_by_ip:
        rate_limit:
          limit_type: ip
          requests: 100
          duration: 1m
FieldDefaultDescription
limit_typeallall, ip, or realip. Ignored when key is set.
requests100Number of requests allowed per duration.
duration1mRate-limit window.
keyWhat requests are counted by, see below. Several parts are combined.
trusted_proxiesCIDRs whose X-Forwarded-For / X-Real-IP headers are used for the ip key.
headersfalseAdd X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset to responses.
redisShare counters between Turna instances, see below. Default is in memory.
fail_closedfalseReject with 503 when Redis is unavailable instead of letting requests through.

Rejected requests get 429 Too Many Requests with a Retry-After header.

ip uses the connection address, or the client behind one of trusted_proxies. realip keys on proxy-supplied headers (True-Client-IP, X-Real-IP, then the left-most X-Forwarded-For) from every peer, falling back to the connection address; clients can forge it unless a proxy in front always overwrites these headers. Prefer ip with trusted_proxies.

Keys ​

KeyCounted by
ipClient IP, see trusted_proxies.
realipClient IP from forwarding headers of any peer.
pathRequest path.
hostRequest host.
methodRequest method.
header:<name>Header value, e.g. header:X-API-Key.
query:<name>Query parameter value.
cookie:<name>Cookie value.

A part that is missing in the request falls back to the client IP, so leaving out a header does not escape the limit.

yaml
rate_limit:
  key:
    - header:X-API-Key
    - path
  requests: 1000
  duration: 1h
  headers: true

Redis ​

With several Turna instances behind a load balancer, each in-memory limiter counts on its own. Redis shares the counters:

yaml
rate_limit:
  limit_type: ip
  requests: 100
  duration: 1m
  redis:
    address:
      - redis:6379
    password: secret
    key_prefix: turna_ratelimit_
FieldDefaultDescription
redis.addressServer addresses. More than one selects Redis Cluster.
redis.username / redis.passwordCredentials.
redis.clusterfalseForce cluster mode for a single address; otherwise it is detected.
redis.gcp_iamfalseAuthenticate with a Google Cloud access token (Memorystore IAM), see session. Leave username and password empty.
redis.tlsenabled, cert_file, key_file, ca_file.
redis.key_prefixturna_ratelimit_Prefix of the counter keys.

Redis must be reachable at startup. If it fails later, requests pass unless fail_closed is set.

rate_limit.redis cannot be set from Docker labels.