We'll automatically use the x-forwarded-for header to determine the client's IP address when behind a proxy.
If you're publicly exposing your server without a load balancer in front of it, you should set the AIKIDO_TRUST_PROXY env var to false to ensure that the correct IP address is used. Otherwise, someone could potentially spoof their IP address by adding the above header and thus bypassing the rate limiting.
If you need to use a different header to determine the client's IP address, you can set the AIKIDO_CLIENT_IP_HEADER environment variable to the name of that header. This will override the default x-forwarded-for header.
# For DigitalOcean App Platform
AIKIDO_CLIENT_IP_HEADER=do-connecting-ip flask --app app run The AIKIDO_TRUST_PROXY environment variable controls whether Zen reads the IP header at all, and how it selects the client IP from it.
| Value | Behavior |
|---|---|
(not set) or true |
Default. Trust the header and use the first non-private IP found, scanning from right to left. Suitable for most deployments behind a reverse proxy. |
false |
Ignore the header entirely and always use the remote address set by the server or middleware. Use this when your server is directly exposed to the internet without a proxy in front of it. |
1.2.3.4/32, … (CIDR notation) |
Only trusted if the request's remote address matches one of the specified CIDR ranges; otherwise the header is ignored and the remote address is used. When trusted, use the first non-private IP that does not belong to any of the specified CIDR ranges, scanning from right to left. Multiple ranges can be separated by commas. Use this when your proxies have known public IP addresses. |
1, 2, … (positive integer n) |
Trust the header and use the n-th IP from the right (1 = rightmost). Use this when a fixed number of trusted proxies are in front of your server. Private IPs are not skipped. |
1 selects the rightmost entry; use true to select the rightmost public IP instead.
Warning
The default mode works when the client is the rightmost non-private address in the header. If a public proxy appears to its right, such as a CDN in front of your load balancer, use the CIDR or number-of-proxies mode above. If your clients use private addresses, use the number-of-proxies mode. If your server is directly exposed to the internet, set AIKIDO_TRUST_PROXY=false.
If the server resolves the client IP, as Uvicorn does for connections from --forwarded-allow-ips (127.0.0.1 by default), set AIKIDO_TRUST_PROXY=false so that Zen uses that IP, and configure the server to trust only your own proxies. The CIDR mode can't check the original connection then, because the server replaced it.
If you use Werkzeug's ProxyFix, set AIKIDO_TRUST_PROXY to the same number as its x_for (1 by default), so that Zen picks the same client IP:
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=2) # with AIKIDO_TRUST_PROXY=2Zen picks the IP from the header itself, so this works whether ProxyFix wraps app.wsgi_app or the whole app.