I have a flask application using nginx for a reverse proxy/ssl termination, but I'm running into trouble when using url_for and redirect in flask.
nginx.conf entry:
location /flaskapp {
proxy_pass http://myapp:8080/;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
The idea is that a user navigates to
https://localhost:port/flaskapp/some/location/here
and that should be passed to flask as
http://localhost:8080/some/location/here
This works reasonably well when navigating to a defined route, however if the route has redirect(url_for('another_page'))
, the browser is directed to
http://localhost:8080/another_page
And fails, when the URL I actually want to go to is:
https://localhost:port/flaskapp/another_page
I have tried several other answers for similar situations, but none have seemed to be doing exactly what I am doing here. I have tried using _external=True
, setting app.config['APPLICATION_ROOT'] = '/flaskapp'
and many iterations of different proxy_set_header
commands in nginx.conf
with no luck.
As an added complication, my flask application is using flask-login
and CSRF cookies. When I tried setting APPLICATION_ROOT
the application stopped considering the CSRF cookie set by flask-login
valid, which I assume has something to do with origins.
So my question is, how do I make it so that when flask is returning a redirect()
to the client, nginx understands that the URL it is given needs flaskapp
written into it?