-2

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

  • Could you please add nginx `access.log`? – Andromeda Mar 19 '23 at 04:02
  • Also result of `nginx -T`. – Andromeda Mar 19 '23 at 06:50
  • Thanks for your reply! sudo nginx -t Gives: nginx: [warn] conflicting server name "192.168.50.192" on 0.0.0.0:80, ignored nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful The assess.log is: Blank! The error.log is: 2023/03/19 08:20:29 [warn] 968#968: conflicting server name "192.168.50.192" on 0.0.0.0:80, ignored 2023/03/19 08:23:14 [warn] 1332#1332: conflicting server name "192.168.50.192" on 0.0.0.0:80, ignored – ArtAsDevice Mar 19 '23 at 13:36

2 Answers2

0

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.

Andromeda
  • 1,205
  • 1
  • 14
  • 21
  • If you scroll back to my original posting and reference my two files in sites-available you will see that yes they both have the same 'server_name' so changing the file 'annsquared' to server_name annsquare does cause the creative_space file to be root and gets rid of the 'Welcome to Nginx' but creates another issue in that the path structure is incorrect. The path structure is: http://192.168.50.192/static/img/portfolio/thumbnails/1a.jpg which leaves out the directory creative_space, it should be: http://192.168.50.192/creative_space/static/img/portfolio/thumbnails/1a.jpg – ArtAsDevice Mar 19 '23 at 16:03
  • The questions remains how to make the second Flask app (192.168.50.192:8001) be reached at 192.168.50.192/annsquared. I tried to edit the hosts file by adding: 192.168.50.192:8001 annsquared but that did not work? Thanks again for your help! – ArtAsDevice Mar 19 '23 at 16:32
  • Ok I did fixed the path issue by editing the creative_space file with this change: location /creative_space { root /home/myname/creative_space/static/; expires 30d; } – ArtAsDevice Mar 20 '23 at 02:33
  • Still looking for how to create a sub domain configuration in Flask/Nginx even though the first part of the question got resolved... any help would be appreciated. The IP 192.168.50.192 now uses the Flask app 1 but would like to understand how to get 192.168.50.192/annsquared to display the 2nd app. Thanks! – ArtAsDevice Mar 20 '23 at 17:49
  • Put both in the same server block with the `server_name 192.168.50.192`. – Andromeda Mar 20 '23 at 20:13
  • Could you be more specific? Flask app1 is now 192.168.50.192 and it's config is: `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; } location /creative_space { root /home/annprim/creative_space/static/; expires 30d; } }` The 2nd Flask app is the same IP but port 8001, is this all suppose to go in one file, if so, where would you add to my current file? Thanks! – ArtAsDevice Mar 20 '23 at 21:56
  • Thank you for your suggestion but could you explain how to do that? Thanks! – ArtAsDevice Mar 21 '23 at 16:24
  • I really want to help you. But I do not know what is the problem, now. – Andromeda Mar 22 '23 at 15:34
  • Hello again and thank you. Your suggestion to 'put both in the same server block with server_name 192.168.50.192' is confusing. I have two nginx config files, creative_space and the other annsquared. Are you saying I should add the config info from annsquare to the creative_space file or separately edit each file putting the server_name back to 192.168.50.192 which caused the initial error? If you could give me example, that would be helpful? Just trying to have the 2nd Flask app 'annsquared' be reached at 192.168.50.192/annsquared Thank you! – ArtAsDevice Mar 22 '23 at 16:06
0

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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 04 '23 at 22:24