R
Railway8mo 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
Fragly8mo ago
As I understand there is a way of obtaining it, but railway really does not want you to
Solution
ThallesComH
ThallesComH8mo 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
Fragly8mo ago
oh I misunderstood, thought you meant the IP of your running service, my bad
jajoju
jajoju8mo ago
Thanks let me check
Brody
Brody8mo 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
jajoju8mo ago
No I am exposing directly the railway ip
Brody
Brody8mo ago
explain
ThallesComH
ThallesComH8mo 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
jajoju8mo ago
it' works perfertly thanks a lot @ThallesComH
Brody
Brody8mo ago
explain please, if I understand you correctly, you may be doing something wrong
jajoju
jajoju8mo 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
Brody8mo ago
okay, dont know why you mentioned the "railway ip" then
jajoju
jajoju8mo ago
I wanted to say the Railway URL.
Brody
Brody8mo ago
gotcha
Want results from more Discord servers?
Add your server
More Posts