Home > OS >  Running multiple Django apps under same domain with Gunicorn & Nginx?
Running multiple Django apps under same domain with Gunicorn & Nginx?

Time:12-27

I have a static website that works domain.com and a Django project that works domain.com/djangoproject1, now I'm trying to add another Django project as domain.com/djangoproject2, this is my sites-avaliable

    server {
    listen 80;
    listen [::]:80;
    server_name domain.com www.domain.com;
    return 301 https://$host$request_uri;
}

#upstream djangoapp1 { # This did not work
#    server 127.0.0.1:9000 fail_timeout=0;
#}

#upstream djangoapp2 { # This did not work
#    server 127.0.0.1:7000 fail_timeout=0;
#}


server {
  listen 443 ssl;
  server_name ...;
  ssl_certificate ...
  ssl_certificate_key ...;

  root /var/www/portfolio; #Serves static portfolio and works
  index index.html; #Serves static portfolio and works

  location / {
    try_files $uri $uri/ =404;
  }
  
  location /djangoapp1static/ { #Works for app 1 and matches settings
      alias /home/djangoapp1...
  }

  location /djangoapp2static/ {
      alias /home/djangoapp2...
  }

  location /djangoapp1{
    alias /home/djangoapp1/src/;
    include proxy_params;

    proxy_pass http://unix:/run/gunicorn.sock; #WORKS
    #proxy_pass http://djangoapp1; #Does not work even without the second app
    #proxy_pass http://127.0.0.1:9000; #Does not work even without the second app
    

  }

  location /djangoapp2{
    alias /home/djangoapp2/src/;
    include proxy_params;


    proxy_pass http://unix:/run/djangoapp2.sock; #This works for djangoapp1 but not for this one


    #proxy_pass http://djangoapp2; #Tried did not work
    #proxy_pass http://127.0.0.1:7000; #Tried did not work

  }


}

Currently the djangoapp1 works normally while the djangoapp2 works only when I manually run it with gunicorn --bind 127.0.0.1:7000 config.wsgi so it must have something to do with gunicorn djangoapp2.sock exists, the sites-avaliable is linked to sites-enabled so not a problem.

when I sudo systemctl status djangoapp2 I get

    × djangoapp2.service - gunicorn daemon
     Loaded: loaded (/etc/systemd/system/djangoapp2.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Fri 2022-12-23 08:04:16 UTC; 41min ago
   Duration: 462ms
TriggeredBy: × djangoapp2.socket
   Main PID: 47712 (code=exited, status=1/FAILURE)
        CPU: 455ms

.service files are changed and named correspondingly to the apps

according to https://www.ryancheley.com/2021/03/07/setting-up-multiple-django-sites-on-a-digital-ocean-server/ what I'm trying to do should be possible

Is anyone hosting mutiple Django apps like I'm trying to do? I do not want djangoapp1.domain.com and djangoapp2.domain.com, I want them to be /djangoapp1 and /djangoapp2

CodePudding user response:

I needed two instances of gunicorn, each in its own venv instead of using the global usr/bin/gunicorn. Re-created .service and .socket files, start -> enable -> reload everything, it works.

  • Related