A Traefik plugin that intelligently sets the X-Real-Ip header by selecting a configurable IP address (by index/depth) from the X-Forwarded-For header. By default, it uses the first IP, ensuring your backend services see the correct client IP, even behind multiple proxies! π
For incoming requests, this plugin:
X-Forwarded-For header.depth (index). Defaults to depth: 0 (the first IP).X-Real-Ip with that value if the depth is valid for the list of IPs.Add to Static configuration
Ensure you are using the latest version
## Static configurationexperimental:plugins:traefik-xff-to-xrealip:moduleName: github.com/jeppestaerk/traefik-xff-to-xrealipversion: v0.1.3
Remember to add your proxy IPs to the forwardedHeaders.trustedIPs entryPoint configuration in Traefik. Without this, Traefik won't trust the X-Forwarded-For header from your proxies, and this plugin won't work properly.
## Static configurationentryPoints:web:address: ":80"forwardedHeaders:trustedIPs:- "127.0.0.1/32"- "192.168.1.5" # Your proxy IP eg. the ip of the machine running Cloudflare tunnel- "172.16.0.0/16" # trust everytihing from docker eg. if running Cloudflare tunnel in docker containerwebsecure:address: ":443"forwardedHeaders:trustedIPs:- "127.0.0.1/32"- "192.168.1.5" # Your proxy IP eg. the ip of the machine running Cloudflare tunnel- "172.16.0.0/16" # trust everytihing from docker eg. if running Cloudflare tunnel in docker container
Ensure you add forwardedHeaders.trustedIPs to all your entryPoints, especially if you redirect HTTP to HTTPS.
Add to dynamic configuration
## Dynamic configurationhttp:middlewares:xff2realip: # Name your middleware instanceplugin:traefik-xff-to-xrealip: {} # Default depth (0)# or for a custom depth:# traefik-xff-to-xrealip:# depth: 1
## Dynamic configurationhttp:routers:my-app:rule: Host(`myapp.example.com`)service: my-appmiddlewares:- xff2realip@file
X-Forwarded-For: 203.0.113.5, 10.0.0.1, 192.168.1.100
http:middlewares:xff2realip:plugin:traefik-xff-to-xrealip: {}
depth: 0):X-Real-Ip: 203.0.113.5
depth: 1)X-Forwarded-For: 203.0.113.5, 10.0.0.1, 192.168.1.100
http:middlewares:xff2realip:plugin:traefik-xff-to-xrealip:depth: 1 # Selects the second IP (index 1)
depth: 1):X-Real-Ip: 10.0.0.1
This plugin must be built or declared through Traefik's experimental plugin system.
You can run Traefik with plugins using Docker, Kubernetes, or binary.
go test ./...
GitHub Actions CI is set up to run build and tests on every commit or pull request.
MIT © 2025 Jeppe Stærk