Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx + gunicorn: deploy more than one Flask application [duplicate]

I already asked this in the Arch Linux boards but didn't get no answer. So I am trying my luck over here:

I am trying to set up nginx + gunicorn on my Arch Linux server to run multiple Flask apps. However I am seemingly failing in configuring nginx the right way to do so. When I just got one Flask app up and running everything seems to work fine. I included the /etc/nginx/sites-available and /etc/nginx/sites-enabled in my /etc/nginx/nginx.conf. I created a file "flask_settings" 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 app (sample App, hellp.py) which i run with gunicorn in a virtual environment. I just run it using

gunicorn hello:app

If i visit my servers IP I can access the file and the different routes. Now I tried to set up another app 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;

}


}

I then try to run the app inside its very own virtual environment with

   gunicorn --bind 127.0.0.1:8001 hello:app

When I restart nginx afterwards, I am still able to access the first app and all of its routes, but if I try to access the other one by entering my servers IP + the router (after the "/"), nginx always tells me, that the sites cannot be found. Am I missing anything here? Any help is highly appreciated. Thanks in advance!

like image 440
Maddin S. Avatar asked May 27 '17 09:05

Maddin S.


1 Answers

You should have seperate proxy location for both apps. i.e, have one nginx conf file but multiple locations for each route or you can have seperate conf file for each.

For example: h1.example.com proxy to a location to the required address with the port number h2.example.com proxy to the location of the second app.

like image 124
T K Sourabh Avatar answered Sep 23 '22 01:09

T K Sourabh