skip to Main Content

I have a Virtual server running Ubuntu 22.04. Nginx is already installed. I have created two separate directories for my Flask apps. The structure for Flask app 1 is: /home/myname/creative_space/ inside that directory I have created a venv and activated it so I could install via pip Gunicorn, Flask, Flask_bootstrap. Also in this directory is app.py, a ‘static’ directory, a ‘templates’ directory and a wsgi.py file. This structure has been duplicated for the 2nd Flask app and is here: /home/myname/annsquared

The virtual server has assigned an IP of 192.168.50.192
Both sites can be accessed by doing the following: Flask app 1 is 192.168.50.192:8000 and the second Flask app 2 is 192.168.50.192:8001
Additionally I created two files in /etc/nginx/sites-available/ the files are named ‘creative_space’ and the other ‘annsquared’

The contents for creative_space is as follows:


server {
    listen 80;
    server_name 192.168.50.192;    

    location / {
        proxy_pass http://127.0.0.1:8000/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        
    }
}

The contents for annsquared is as follows:

server {
    listen 80;
    server_name 192.168.50.192;

    location /annsquared {
        proxy_pass http://127.0.0.1:8001/annsquared;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }


    location /static/ {
        root /home/myname/annsquared/static/;
        expires 30d;
    }
}

Both files are linked to sites-enabled.

Also I created two files in /etc/systemd/system the files are named ‘creative_space.service’ and the other ‘annsquared.service’

The content for creative_space.service is as follows:

[Unit]
Description=Gunicorn instance for a Flask app
After=network.target

[Service]
User=myname
Group=www-data
WorkingDirectory=/home/myname/creative_space
Environment="PATH=/home/myname/creative_space/venv/bin"
ExecStart=/home/myname/creative_space/venv/bin/gunicorn --workers 3  --bind 0.0.0.0:8000 --error-logfile /var/log/creative_space/error.log wsgi:app

[Install]
WantedBy=multi-user.target

The content for annquared.service is as follows:

[Unit]
Description=Gunicorn instance to serve annsquared
After=network.target

[Service]
User=myname
Group=www-data
WorkingDirectory=/home/myname/annsquared
Environment="PATH=/home/myname/annsquared/venv/bin"
ExecStart= /home/myname/annsquared/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8001 --error-logfile /var/log/annsquared/error.log wsgi:app

[Install]
WantedBy=multi-user.target

Everything works fine and can be accessed at 192.168.50.192:8000 and 192.168.50.192:8001

What I’m trying to achieve is that the first Flask app (192.168.50.192:8000) can be the root reached at 192.168.50.192 and the second Flask app (192.168.50.192:8001) can be reached at 192.168.50.192/annsquared.

Apologizes for the length of this post but giving the most details upfront usually eliminates a lot of back and forth. Thanks in advance for your guidance!

I have deleted the ‘default’ file in sites-available and in sites-enabled but still getting the ‘Welcome to Nginx’ greeter at 192.168.50.192

2

Answers


  1. Chosen as BEST ANSWER

    My initial questions concerned getting my Flask app 1 to default to the virtual server IP and the second part of the question was to have the 2nd Flask app to be a name, (annsquared) as part of the default IP such as 192.168.50.192/annsquared. Perhaps I wasn't able to articulate my issues clearly so others might answer but I did sort out my problems with a different approach. As I initially stated my flask app1 and flask app2 were able to be accessed by 192.168.50.197:8000 and 192.168.50.197:8001 without error, so I 'unlink' the default file in sites-available and removed it from sites-enabled. I then created and edited /etc/nginx/conf.d/new_sites.conf (you can call the file whatever you want). The basic instructions are from this video a link! I also edited my /etc/hosts file on the virtual and host machine to reflect these names, an example would be 192.168.50.192 annsquared 192.168.50.192 posters. Now I can reach my flask app sites by name. http://annsquared and the 2nd flask site http://posters Of course this is only for local development. Hope this clears things up.

    I was requested to add more details. What I previously wrote and the link I supplied pretty much sums up my solution. To get one of the IPs to be the default I needed to have unique 'server_name', mine were both the same. To have a 'word name' for the url instead of IP:PORT I followed the instructions from the supplied link in my post. That's it.


  2. I think the issue is you have 2 server blocks with the same server_name. Defining 2 locations in the same server should resolve your conflict.This link helps you about the issue more.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search