'How to proxypass Apache to a dockerized Flask/Gunicorn app

We have a server running apache2 that is hosting a number of websites and applications. We have a dockerized Flask application (served with Gunicorn) that we would like to deploy on the server and have Apache direct traffic to it. Basically https://oneofoursites.com/flask_app redirects to the port Docker is exposing for the Flask app. Right now, the site works fine if you navigate to https://oneofoursites.com:4000/.

I first attempted something like this in a apache.conf under sites-available:

ProxyPass /flask_app http://127.0.0.1:4000/ connectiontimeout=3600 timeout=3600
ProxyPassReverse /flask_app http://127.0.0.1:4000/
<Location /flask_app >
    Require all granted
</Location>

The flask app is now available at https://oneofoursites.com/flask_app, but none of the links work.

The flask documentation was slightly helpful, but not close enough to my case. I cannot seem to get Flask to pick up the environment variables (since it is inside a Docker container), so couldn't get it to reform the headers itself. I looked into mod_wsgi, but am unsure how to deploy it (inside the Docker container and Apache won't run it; outside the container how do I get it to redirect to the Flask app?). Most guides I am finding are recommending Nginx, so perhaps I need to deploy Nginx (either in the existing Flask container or a separate one with docker-compose)? Would it then act as a suitable go between for Apache and the Flask app?



Solution 1:[1]

You need to set the env-variable SCRIPT_NAME to your url-path when starting gunicorn. In your case something like

gunicorn -e SCRIPT_NAME=/flask_app --bind 127.0.0.1:4000 app:app

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CoderColor