I have a Python 3 app on Google App Engine Standard.
I was using request.remote_addr
to get users' IP addresses and it was always returning 127.0.0.1.
I then added werkzeug ProxyFix like this:
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
I'm also using other middleware to enable cloud ndb:
app.wsgi_app = ndb_wsgi_middleware(app.wsgi_app)
Now request.remote_addr
always returns 169.254.1.1 which is a link-local IP address.
Is there a way to fix ProxyFix to get Flask to return the correct IP address?
The X-Forwarded-For
header does have the right IP address, but I'd like to get this IP address in request.remote_addr
.
It looks like app engine has two proxies. Don't know what the second one is (one is for the load balancer).
The solution is to tell ProxyFix
to trust both proxies, and you do that this way:
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With