When traefik is deployed behind multiple load balancers, this plugin can be used to detect different load balancers and extract the real IP from different header fields, then output the value to the x-real-ip header.
This plugin can prevent IP spoofing by checking if the values form the received header information of the load balancer match before extracting the IP address.
For example, in the configuration of CloudFlare load balancer shown below, we configure it to only accept the header x-from-cdn with a value equal to cf-foo, and extract the IP address from the Cf-Connecting-Ip header. Since users never know about the existence of the x-from-cdn header or its required value cf-foo, it remains secure π‘οΈ. To increase complexity and avoid being guessed, you can use a random string :)
CloudFlare
βββββββββββ
β ββββββββββββββββββββββββββββββββββΊ βββββββββ¬βββββββββ
βββββββββββ x-from-cdn:cf-foo β β β
Cf-Connecting-Ip: realip β β β
CDN2 β β β
βββββββββββ β β paxxs'sβ
β ββββββββββββββββββββββββββββββββββΊ βtraefikβ β x-real-ip:realip
βββββββββββ x-from-cdn:mf-bar β βGet-rea βββββββββββββββΊ
Client-iP: realip β β l-ip β
CDN3 β βPlugin β
βββββββββββ β β β
β βββββββββββββββββββββββββββββββββΊ β β β
βββββββββββ x-from-cdn:mf-fun βββββββββ΄βββββββββ
x-forwarded-for: realip,x.x.x.x
(truthedIP) β² β²
β β
ββββββββββ β β
ββββββββββ βββββββββββββββββββββββββββββββββββββ β
"*" β
ββββββββββ RemoteAddr/etc.. β
ββββββββββ ββββββββββββββββββββββββββββββββββββββββ
E.g. Cloudflare:
Rules > Transform Rules > HTTP Request Header Modification > Add
X-From-Cdn
cf-foo

Plugin Info:
github.com/Paxxs/traefik-get-real-ip[Please fill the latest version !]Traefik Configuration:
pilot:token: [REDACTED]experimental:plugins:real-ip:moduleName: github.com/Paxxs/traefik-get-real-ipversion: [Please fill the latest version !]
http:middlewares:real-ip-foo:plugin:real-ip:enableLog: false # default: false, enable to see detailed logsdeny403OnFail: true # default: false, when true returns 403 if no matching CDN header founderaseProxyHeaders: true # default: false, erase CDN-specific headers after processingProxy:- proxyHeadername: X-From-CdnproxyHeadervalue: mf-funrealIP: X-Forwarded-For- proxyHeadername: X-From-CdnproxyHeadervalue: mf-barrealIP: Client-IpOverwriteXFF: true # default: false, v1.0.2 or above- proxyHeadername: X-From-CdnproxyHeadervalue: cf-foorealIP: Cf-Connecting-IpOverwriteXFF: true # default: false, v1.0.2 or above- proxyHeadername: "*"realIP: RemoteAddrrouters:my-router:rule: Host(`localhost`)middlewares:- real-ip-fooservice: my-serviceservices:my-service:loadBalancer:servers:- url: 'http://127.0.0.1'
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
| enableLog | bool | No | false | Enable detailed logging for debugging purposes |
| deny403OnFail | bool | No | false | When true, returns a 403 Forbidden response if no matching CDN header is found |
| eraseProxyHeaders | bool | No | false | When true, erases CDN-specific headers after processing to prevent leaking CDN identification |
| Proxy | array | Yes | - | Array of proxy configurations |
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
| proxyHeadername | string | Yes | - | The header name to check for CDN identification. Use "*" to match all sources |
| proxyHeadervalue | string | No | - | The expected value for the header. Not required when proxyHeadername is "*" |
| realIP | string | Yes | - | The header to extract the real IP from. Special value "RemoteAddr" will use the connection's remote address |
| OverwriteXFF | bool | No | false | When set to true, also overwrites the X-Forwarded-For header with the extracted real IP (v1.0.2+) |
proxyHeadername with value proxyHeadervalue (or accepts any if proxyHeadername is "*").realIP.X-Real-Ip header.OverwriteXFF is true, the X-Forwarded-For header is also overwritten with the extracted IP.eraseProxyHeaders is true, the plugin removes the CDN-specific headers (the matched proxyHeadername and realIP headers) to prevent leaking CDN identification to downstream services. Standard headers like X-Forwarded-For are preserved.deny403OnFail is set to true, the plugin returns a 403 Forbidden response, preventing further request processing.