Skip to content

bad_behavior ​

bad_behavior bans clients that get many error responses in a short time, such as scanners probing for paths.

yaml
server:
  http:
    middlewares:
      ban_scanners:
        bad_behavior:
          threshold: 30
          window: 1m
          ban_time: 24h
          allow:
            - 10.0.0.0/8
          trusted_proxies:
            - 172.16.0.0/12
FieldDefaultDescription
status_codes400,401,403,404,405,429,444Response statuses counted as bad. Codes or ranges like 500-599.
threshold30Bad responses within window that ban the client.
window1mCounting window, starting at the first bad response of a client.
ban_time24hHow long a client stays banned. Must be greater than 0.
status_code403Status of requests from banned clients.
allowIPs or CIDRs that are never counted or banned.
trusted_proxiesCIDRs of proxies in front of Turna, see ip_filter.
redisShare counters and bans between Turna instances, see below. Default is in memory.
fail_closedfalseReject with 503 when Redis is unavailable instead of letting requests through.

Requests of a banned client are rejected before they reach the next handler and get a Retry-After header with the remaining ban time. A ban is logged as a warning with the client IP.

Place bad_behavior before the middlewares and services whose responses should be counted. Responses written by middlewares placed before it are not seen.

The in-memory counters and bans are dropped when the configuration is reloaded.

Redis ​

yaml
bad_behavior:
  redis:
    address:
      - redis:6379
    password: secret
    key_prefix: turna_badbehavior_
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_badbehavior_Prefix of the counter and ban keys.

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

bad_behavior.redis cannot be set from Docker labels.