Problem getting real IP from traefik

I'm using docker swarm and I have an http service that redirects traffic from traefik to an nginx container. Inside the nginx config file i have this:

    real_ip_header X-Forwarded-For;
    set_real_ip_from traefik_proxy;

And of course i can ping it:

/ # ping traefik_proxy
PING traefik_proxy (10.0.27.110): 56 data bytes
64 bytes from 10.0.27.110: seq=0 ttl=64 time=0.100 ms

But.... nginx logs still show: 10.0.27.120 - - [26/Jun/2020:18:08:00 +0000] "GET /wp-content/upload.... Notice is not the same IP as the ping one, but still not the user IP.

How can I fix it? Thanks!

I have found the service port needs to be published using the host binding instead of ingress. The side effect being that only the host that is running traefik publishes the port.

This is bypassing the routing mesh

what if ingress passes that variable to traefik? Maybe is traefik not receiving it

Swarm ingress is operating at the TCP/UDP level, it is not able to add headers.

But I'm already using your method I think, and still can't get it to work.

services:
  proxy:
    image: "traefik:latest"
    deploy:
      mode: global
    ports:
      - target: 80
        published: 80
        protocol: tcp
        mode: host
      - target: 443
        published: 443
        protocol: tcp
        mode: host

Try running a containous/whoami container behind traefik, see what header is being set.

It might just be nginx configuration issue.

It works fine with the whoami container, but not with nginx :frowning: any ideas about what can i do? I tried this but does not work: https://stackoverflow.com/a/44648488

I think the problem is nginx getting the real ip from traefik. i don't know how to get it but it's not a problem with docker overlay network since traefik is receiving the correct ip already

EDIT: i think this works fine:

    real_ip_header X-Forwarded-For;
    set_real_ip_from traefik_proxy;

But you need an nginx container with the http_realip_module enabled