0

I have a Django app running with Nginx and Gunicorn on mysite.com. I would like to deploy a second Django app to be reachable under mysite.com/secondapp while my first app still just by mysite.com.

I followed the tutorial from DigitalOcean and my current config for App 1 is:

/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myprojectdir
ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          myproject.wsgi:application

/etc/nginx/sites-available/myproject

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/sammy/myprojectdir;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

My question is: do I need to create 2 separate .socket and .service file for my App 2 and how do I make App 2 accessible by mysite.com/secondapp ?

Margo
  • 348
  • 2
  • 3
  • 13
  • In addition to the answer given by @mgsxman, look at [this](https://stackoverflow.com/questions/20997863/add-a-prefix-to-url-patterns-in-django) Q/A. – Ivan Shatsky Jun 08 '20 at 14:58
  • Thank you very much. It looks exactly like what I need. – Margo Jun 08 '20 at 17:11

1 Answers1

1

Yes you need to create 2 separate .socket and .service files.

Then your Nginx config will look like:

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /static/ {
        root /home/sammy/myprojectdir;
    }
    location = /secondapp/static/ {
        root /home/sammy/myprojectdir-app2;
    }


    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
    location /secondapp/ {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn-app2.sock:/;
    }
}

Note that you have to configure your second app to search for static at correct location.

Update:

All routes on second app should also be changed from /route_path/ to /secondapp/route_path/.

mgsxman
  • 676
  • 5
  • 11
  • Thank you for your answer! Before creating .socket and .service for the second app, I decided to try to add location /firstapp/ { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } to test if its working. For some reason I get "Not Found.The requested URL /firstapp/ was not found on this server." Nginx error log doesnt show anything usefull. – Margo Jun 08 '20 at 15:10
  • Note the trailing `:/` at `proxy_pass http://unix:/run/gunicorn-app2.sock:/;` Because socket root is `/` at which it accept connections. – mgsxman Jun 08 '20 at 15:45
  • 1
    See update, try to change routes at second app, and leave `proxy_pass http://unix:/run/gunicorn-app2.sock;` – mgsxman Jun 08 '20 at 17:51
  • That worked great, without ":/" in the end. Could you please explain me why we didn't need to create a separate entry in "sites-available" for the app2, but append it to to app1 config? – Margo Jun 09 '20 at 09:16
  • 1
    Because you can not use the same `server_name server_domain;` in two different configs. But that will be possible if you are able to use another domain name, like `server_name server_domain-app2;` – mgsxman Jun 09 '20 at 14:04