Remote address overwritten when deploying docker images

Hi, I have a docker image containing angular SPA and nginx. NGINX is configured to pass the remote addr from cloudflare, so that I can see the real ip in the nginx logs. My problem is, that docker overwrites the remote addr, so that in NGINX I'm seeing the docker network's gateway IP and not the real remote addr. Normally network=host solves the issue, when running a docker container. Is there a way to pass that docker run parameter to railway app? Thanks. 😃
Solution:
now that you're mentioning it, perhaps I need to trust the railway proxy ips too
Jump to solution
38 Replies
Percy
Percy10mo ago
Project ID: 71de5038-f8d1-4214-81d4-290481deb483
leiferksn
leiferksnOP10mo ago
71de5038-f8d1-4214-81d4-290481deb483
Brody
Brody10mo ago
you would need to have nginx trust the applicable header that cloudflare sets https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-connecting-ip
leiferksn
leiferksnOP10mo ago
I've already done that. It didn't help, because I'm deploying a docker image and docker changes it. https://forums.docker.com/t/docker-overrides-remote-addr-and-forwarded-for-headers-to-the-bridge-network-ip/138485
Docker Community Forums
Docker overrides remote_addr and forwarded_for headers to the bridg...
As described in the title and in numerous posts on github and the docker community forums nginx is unable to fetch and log in the access.log the original requesting ip and the remote_addr and forrwarded_for headers are always equal to the docker network’s ip. Is there any progress on rectifying this issue besides “hacky” approaches posted year...
Brody
Brody10mo ago
I'm sorry but docker is not overriding the CF-Connecting-IP header that cloudflare is setting, you have the wrong idea there
leiferksn
leiferksnOP10mo ago
no docker overwrites the $remote_addr after I've set it with the value from CF-Connecting-IP with nginx. I can reproduce this behaviour locally.
Brody
Brody10mo ago
that would be down to a misconfiguration. instead, I highly recommend caddy. with caddy you can very easily set the header to that of what cloudflare sets https://caddyserver.com/docs/caddyfile/options#client-ip-headers and then set the trusted ips to accept the header from https://caddyserver.com/docs/caddyfile/options#trusted-proxies https://www.cloudflare.com/en-gb/ips/ I have a few example Caddyfiles and straightforward methods to serve your app with caddy, so let me know if you're interested in that!
leiferksn
leiferksnOP10mo ago
I don't have a problem with the configuration, because I'm seeing the value from cloudflare. My problem is that I'm not able to setup a network for my docker image on railway, the way I can set it up locally, by passing --net=host when I run the container.
Brody
Brody10mo ago
in NGINX I'm seeing the docker network's gateway IP and not the real remote addr

I don't have a problem with the configuration, because I'm seeing the value from cloudflare
im confused, can you or can you not see the correct ip in your logs?
leiferksn
leiferksnOP10mo ago
No, I can't see the correct IP in my logs.
Brody
Brody10mo ago
then why was this said?
I'm seeing the value from cloudflare
you are giving very conflicting information
leiferksn
leiferksnOP10mo ago
that is my problem. and the solution that I've figured out locally was to setup my docker differently. hence the question - is it possible to do that on railway as well
Brody
Brody10mo ago
of course not, but with the correct configuration there would be no need to
leiferksn
leiferksnOP10mo ago
ok, and what would be the correct configuration for my case - nginx + angular2 in a docker image? I've already set up remote ip mod for nginx and it's working on my local machine but only if I add --net=host to docker run
Brody
Brody10mo ago
what ip are you seeing then?
leiferksn
leiferksnOP10mo ago
127.0.0.1
Brody
Brody10mo ago
yeah thats definitely a misconfiguration, id be happy to help you get this setup with caddy!
leiferksn
leiferksnOP10mo ago
I'm not familiar with caddy, is there no example for nginx?
Brody
Brody10mo ago
i dont either of us are too familiar with nginx, i only have examples for caddy, so ill try to get a proof of concept working with caddy
leiferksn
leiferksnOP10mo ago
ok, thanks. I don't think I would be using caddy now. I'll try and figure out a way with nginx.
Brody
Brody10mo ago
caddy is pretty simple, may i ask why the want to use nginx instead? its config is obviously giving you a bit of troubles
leiferksn
leiferksnOP10mo ago
because I don't want to switch to yet another new technology and also I'm not convinced that this is a web server/reverse proxy configuration issue, since I'me getting a result by running docker differently.
Brody
Brody10mo ago
i assure you this isnt down to railway doing anything incorrectly
leiferksn
leiferksnOP10mo ago
I'm not suggesting that. I just need to understand how railway handles docker networks. by default docker uses bridge network
Brody
Brody10mo ago
im not seeing how docker comes into play here as long as your configurations are correct, your app sits behind a proxy on railway, and then another proxy from cloudflare, you simply need to trust the local ips and cloudflares ips, then pull the client ip from the header that cloudflare sets
leiferksn
leiferksnOP10mo ago
Docker Community Forums
Docker overrides remote_addr and forwarded_for headers to the bridg...
As described in the title and in numerous posts on github and the docker community forums nginx is unable to fetch and log in the access.log the original requesting ip and the remote_addr and forrwarded_for headers are always equal to the docker network’s ip. Is there any progress on rectifying this issue besides “hacky” approaches posted year...
leiferksn
leiferksnOP10mo ago
cloudflare -> docker network -> nginx -> docker container in docker network happens the thing that's descriped in the post
Brody
Brody10mo ago
cloudflare -> railway proxy -> nginx (your app)
leiferksn
leiferksnOP10mo ago
yes railway proxy also somewhere in between, but I'm sure railway is passing headers correctly. I'm seeing that. what's messing the headers is the docker network I think
Brody
Brody10mo ago
why not just straight log the header? please remove this notion of the docker network causing you issues, i get that you can change it locally and have it work, but with the correct configurations you do not need to change the network. again, i am still offering you a drop in pre-made solution with caddy.
Brody
Brody10mo ago
with caddy, logging the client ip thats set by cloudflare is as simple as a 1 line config
No description
No description
leiferksn
leiferksnOP10mo ago
exactly the same thing I'm doing in nginx set_real_ip_from ... trusted ips ... real_ip_header cf-connecting-ip; real_ip_recursive on; 🙂
Brody
Brody10mo ago
what ips are you trusting?
Solution
leiferksn
leiferksn10mo ago
now that you're mentioning it, perhaps I need to trust the railway proxy ips too
Brody
Brody10mo ago
I thought you where already doing that, my bad
leiferksn
leiferksnOP10mo ago
In the end it worked by trusting the railway IPs. However the really ngx_http_realip_module didn't store the value where it says in the documentations it would. Instead in realip_remote_addr it was stored in $remote_addr. It took me also some time to figure this out. Very strange. Thank you for talking to me. It helped clearing up the the issue. 😀
Brody
Brody9mo ago
happy to help where I can!
Want results from more Discord servers?
Add your server