8

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!

Maddin S.
  • 111
  • 1
  • 4
  • 2
    I don't agree that this a duplicate question. Running gunicorn in several directories and setting nginx as a reverse proxy is not direction of that other post. – pauljohn32 Feb 05 '20 at 05:03

2 Answers2

3

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.

T K Sourabh
  • 351
  • 2
  • 8
  • So you mean create one file and add two – Maddin S. May 27 '17 at 10:12
  • Yeah, in one ngnix file serve two applications. for each app pass proxy to respective port on which you ran the app. [This](https://stackoverflow.com/questions/13660118/running-a-flask-app-with-nginx-and-gunicorn?rq=1) might be helpful – T K Sourabh May 27 '17 at 12:02
0

Using the approach you have, is not the recommended way to do it. Consider making a .sock file as explained in this tutorial(Although this is for Ubuntu, It can be adapted to Arch).

Another thing, (probably more important). Your server block is missing the server name. So Nginx does not know which URL to respond to.

Unless you have the server on the same computer as your local machine, 127.0.0.1 should not be accessible from the browser. Consider passing the parameter as : app.run(host = '0.0.0.0').

When it comes to the second site file. The location of the root (e.g: https://google.com/) is not mentioned. So Nginx cannot even reach the root. Again, the server name block should be mentioned for this one too.

Hope this helps.

  • Okay, i followed this approach (the one mentioned in the tutorial) and for one app this works perfectly fine. But it doesn't seem to work, if i am trying the same technique for another flask app accessible under a subdomain. For the first one I visit the IP of my server (e.g. 1.2.3.4/) and it shows me the page. For the second one I want to visit 1.2.3.4/flask but it always returns "Page not found". Although I copied the files from app one and edited the "location" in the nginx config to be /flask, not simply /. Which part do I need to edit to point that app to the /flask subdomain of my IP? – Maddin S. May 28 '17 at 18:41
  • You cannot redirect a separate endpoint to a different flask server. One flask server (generally) responds to one IP:Port Combination. So, your solutions would either be to combine the flask apps(which is obviously not fit for all cases) or get a domain and point two separate subdomains to two virtual hosts on Nginx. – Aditya Walvekar May 29 '17 at 07:24