If Traefik is behind a load balancer, it won't be able to get the Real IP from the external client by checking the remote IP address.
This plugin solves this issue by overwriting the X-Real-Ip with an IP from the X-Forwarded-For or Cf-Connecting-Ip (if from Cloudflare) header. The real IP will be the first one that is not included in any of the CIDRs passed as the ExcludedNets parameter. The evaluation of the X-Forwarded-For or Cf-Connecting-Ip (if from Cloudflare) IPs will go from the last to the first one.
pilot:token: xxxxexperimental:plugins:traefik-real-ip:modulename: github.com/soulbalz/traefik-real-ipversion: v1.0.3
http:routers:my-router:rule: Path(`/whoami`)service: service-whoamientryPoints:- httpmiddlewares:- traefik-real-ipservices:service-whoami:loadBalancer:servers:- url: http://127.0.0.1:5000middlewares:traefik-real-ip:plugin:traefik-real-ip:excludednets:- "1.1.1.1/24"
kind: DeploymentapiVersion: apps/v1metadata:namespace: defaultname: traefiklabels:app: traefikspec:replicas: 1selector:matchLabels:app: traefiktemplate:metadata:labels:app: traefikspec:terminationGracePeriodSeconds: 60serviceAccountName: traefik-ingress-controllercontainers:- name: traefikimage: traefik:v2.4args:- --api.insecure- --accesslog- --entrypoints.web.Address=:80- --providers.kubernetescrd- --pilot.token={YOUR_PILOT_TOKEN}- --experimental.plugins.traefik-real-ip.modulename=github.com/soulbalz/traefik-real-ip- --experimental.plugins.traefik-real-ip.version=v1.0.3ports:- name: webcontainerPort: 80- name: admincontainerPort: 8080resources:requests:cpu: 300mlimits:cpu: 500m---apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: traefik-real-ipspec:plugin:traefik-real-ip:excludednets:- "1.1.1.1/24"---apiVersion: traefik.containo.us/v1alpha1kind: IngressRoutemetadata:name: ingress-examplenamespace: defaultspec:entryPoints:- webroutes:- kind: Rulematch: Host(`domain.ltd`) && PathPrefix(`/`)services:- name: example-serviceport: 80middlewares:- name: traefik-real-ip
Supported configurations per body
Setting | Allowed values | Required | Description |
---|---|---|---|
excludednets | []string | No | IP or IP range to exclude forward IP |