R
Railway14mo ago
jajoju

Obtaining Client's Public IP Address in Flask Docker Container on Railway.app

I have a Docker container on Railway.app with a Flask app running on Gunicorn. When I try to view the client's IP address, it provides a local IP rather than a public one. What could be the reason, and how can I solve it? Thank you. gunicorn --workers=1 --bind 0.0.0.0:5001 --preload 'wsgi:create_app()' --timeout 90
Solution:
Railway will foward a header called "X-Envoy-External-Address". you've to tell flask to trust that. example: ```py if request.headers.get("X-Envoy-External-Address"):...
Jump to solution
14 Replies
Fragly
Fragly14mo ago
As I understand there is a way of obtaining it, but railway really does not want you to
Solution
ThallesComH
ThallesComH14mo ago
Railway will foward a header called "X-Envoy-External-Address". you've to tell flask to trust that. example:
if request.headers.get("X-Envoy-External-Address"):
ip = request.headers.get("X-Envoy-External-Address")
else:
ip = request.remote_addr
if request.headers.get("X-Envoy-External-Address"):
ip = request.headers.get("X-Envoy-External-Address")
else:
ip = request.remote_addr
Fragly
Fragly14mo ago
oh I misunderstood, thought you meant the IP of your running service, my bad
jajoju
jajojuOP14mo ago
Thanks let me check
Brody
Brody14mo ago
I'd like to note thalles's solution is only applicable if railway is the only proxy your app sits behind, if you where for example running your app behind cloudflare's proxy you would want to read the CF-Connecting-IP header instead
jajoju
jajojuOP14mo ago
No I am exposing directly the railway ip
Brody
Brody14mo ago
explain
ThallesComH
ThallesComH14mo ago
also, flask has a trust proxy feature, but i don't know it's behavior when a client tries to spoof the x-forwaded-for header. https://flask.palletsprojects.com/en/latest/deploying/proxy_fix/ i would go with the X-Envoy-External-Address if you just want something to work for now.
jajoju
jajojuOP14mo ago
it' works perfertly thanks a lot @ThallesComH
Brody
Brody14mo ago
explain please, if I understand you correctly, you may be doing something wrong
jajoju
jajojuOP14mo ago
I deployed a docker on railway by github, and in settings -> networking there is an url, i am using that url from the front to call the api hosted.
Brody
Brody14mo ago
okay, dont know why you mentioned the "railway ip" then
jajoju
jajojuOP14mo ago
I wanted to say the Railway URL.
Brody
Brody14mo ago
gotcha
Want results from more Discord servers?
Add your server