A Traefik middleware plugin that extracts the real client IP address from various HTTP headers.
Traefik Real IP extracts and validates the actual client IP address from commonly used headers such as X-Forwarded-For, X-Real-IP, and Cf-Connecting-Ip. This plugin is particularly useful when Traefik is behind a CDN, proxy, or load balancer like Cloudflare.
Cf-Connecting-Ip, Eo-Connecting-Ip, X-Real-IP, and X-Forwarded-For headersThe easiest way to install this plugin is through the Traefik Plugin Catalog.
Add the plugin to your Traefik static configuration:
experimental:plugins:traefik-real-ip:moduleName: github.com/zekihan/traefik-real-ipversion: v0.1.20
# Static configurationexperimental:plugins:traefik-real-ip:moduleName: github.com/zekihan/traefik-real-ipversion: v0.1.20
# Dynamic configurationhttp:middlewares:traefik-real-ip:plugin:traefik-real-ip:thrustLocal: truethrustCloudFlare: truethrustEdgeOne: falsetrustedIPs:- "1.2.3.4/32"- "10.0.0.0/8"logLevel: infodenyUntrusted: false
| Option | Type | Default | Description |
|---|---|---|---|
thrustLocal | boolean | true | Trust local and private IP ranges |
thrustCloudFlare | boolean | true | Trust Cloudflare IP ranges |
thrustEdgeOne | boolean | false | Trust EdgeOne IP ranges |
trustedIPs | array of strings | [] | Additional IP ranges to trust in CIDR notation |
logLevel | string | info | Log level (debug, info, warn, error) |
denyUntrusted | boolean | false | Deny requests from untrusted IPs with 403 Forbidden |
denyUntrusted is enabled and the source IP is not trusted, it returns a 403 Forbidden responseCf-Connecting-Ip, Eo-Connecting-Ip, X-Real-IP, then X-Forwarded-For.X-Is-Trusted: yes|no header indicating if the source was trustedIf your server has a public IP but uses a WAF/CDN like Cloudflare, you may want to ensure that traffic can only reach your server through the WAF/CDN. Enable the denyUntrusted option to reject any traffic that doesn't come from trusted IP ranges (such as Cloudflare IPs).
http:middlewares:traefik-real-ip:plugin:traefik-real-ip:thrustCloudFlare: truedenyUntrusted: true
With this configuration, requests that bypass Cloudflare and reach your server directly will receive a 403 Forbidden response.
A Docker Compose setup is provided in the testing folder to test the plugin locally:
cd testingdocker-compose up -d
go test ./...
This project is licensed under the MIT License - see the LICENSE file for details.