Nginx + gunicorn: deploy multiple flask applications
I already asked this on Arch Linux boards but got no answer. So I try myself here:
I am trying to set up nginx + gunicorn on my Arch Linux server to run multiple Flask applications. However, I do not seem to be able to set up nginx on the correct path. When I just had a Flask app everything was working fine. I have enabled / etc / nginx / sites -available and / etc / nginx / sites-enabled in my /etc/nginx/nginx.conf. I created a "flask_settings" file inside / etc / nginx / sites / available and linked it to / etc / nginx / sites -enabled. The file looks like this:
server {
location /{
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
I have a folder containing my Flask application (App example, hellp.py) which I am running with a cannon in a virtual environment. I just run it with
gunicorn hello:app
If I am on my IP servers, I can access the file and different routes. Now I tried to configure another application by creating another file in / etc / nginx / sites -enabled called flask2. It looks like this:
server {
location /hello {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Then I try to run the application in my own virtual environment with
gunicorn --bind 127.0.0.1:8001 hello:app
When I restart nginx after that, I can still access the first application and all its routes, but if I try to access another by entering my servers IP + router (after "/"), nginx always tells me that sites cannot be found. Did I miss something? Any help is greatly appreciated. Thanks in advance!
source to share
You must have a separate proxy location for both applications. that is, have one nginx config file but multiple places for each route, or you can have a separate conf file for each.
For example:
h1.example.com
proxy to the location to the required address with the port number of the
h2.example.com
proxy to the location of the second application.
source to share
Using the approach you have, this is not recommended. Consider creating a file .sock
as described in this tutorial (although this is for Ubuntu, it can be adapted for Arch).
Another matter (perhaps more important). Server block is missing server name. Therefore Nginx does not know which URL to respond to.
If you do not have a server on the same computer as your local computer, 127.0.0.1 should not be accessible from your browser. Consider passing the parameter as: app.run(host = '0.0.0.0')
.
When it comes to the second site file. The root location (ex: https://google.com / ) is not mentioned. So Nginx can't even get to the root. Again, a server name block must be specified for this as well.
Hope it helps.
source to share