Question

How to host multiple Django app with uWSGI and NGINX with sub domains at port 80?

Posted April 14, 2021 1.2k views
NginxDjangoUbuntu 20.04

-1

I’ve been trying to deploy two Django projects on a single server using uWSGI and Unix socket NGINX, I’ve cross-checked the configuration it seems fine, but it behaves oddly. Let me give the project names A for first and B for second for better understanding and referencing.

For both projects, I’ve set up uWSGI and Nginx setup as per this excellent article How To Serve Multiple Django Applications with uWSGI and Nginx in Ubuntu 20.04

Everything is working right emperor.uwsgi.service shows two uwsgi processes for each project A and B there are socket files at the correct path, but when I tried to serve both with the different subdomain, let’s say for A subdomain A.example.com and B subdomain B.example.com at port 80 using Nginx then A.example.com works fine, but B.example.com returns default Nginx page, but if I change the B.example.com’s config and use port 8000 then it works fine at B.example.com:8000.

A.conf symlinked to /etc/nginx/sites-enabled/A from /home/ubuntu/ProjA/nginx/A.conf

upstream A_uwsgi {
    server unix:///run/uwsgi/A.sock fail_timeout=0;
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name A.example.com;

    # charset     utf-8;

    # max upload size
    client_max_body_size 5M;

    access_log /home/ubuntu/ProjA/logs/nginx-access.log;
    error_log /home/ubuntu/ProjA/logs/nginx-error.log;

    # conf for letsencrypt ssl
    include     /home/ubuntu/ProjA/nginx/letsencrypt.conf;

    # Not using since handled via google static storage
    # Django media
    location /media  {
        alias /home/ubuntu/ProjA/media;
    }

    location /static {
        alias /home/ubuntu/ProjA/static;
    }

    # non-media requests handled by uwsgi
    location / {
        include     /home/ubuntu/ProjA/uwsgi/uwsgi_params; # the uwsgi_params file you installed
        uwsgi_pass  A_uwsgi;
    }
}

B.conf symlinked to /etc/nginx/sites-enabled/B from /home/ubuntu/ProjB/nginx/B.conf

upstream B_uwsgi {
    server unix:///run/uwsgi/B.sock fail_timeout=0;
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name B.example.com;

    # charset     utf-8;

    # max upload size
    client_max_body_size 5M;

    access_log /home/ubuntu/ProjB/logs/nginx-access.log;
    error_log /home/ubuntu/ProjB/logs/nginx-error.log;

    # conf for letsencrypt ssl
    include     /home/ubuntu/ProjB/nginx/letsencrypt.conf;

    # Not using since handled via google static storage
    # Django media
    location /media  {
        alias /home/ubuntu/ProjB/media;
    }

    location /static {
        alias /home/ubuntu/ProjB/static;
    }

    # non-media requests handled by uwsgi
    location / {
        include     /home/ubuntu/ProjB/uwsgi/uwsgi_params; # the uwsgi_params file you installed
        uwsgi_pass  B_uwsgi;
    }
}

So as per the above config, the site A.example.com works like a charm but not B.example.com.

but if I change the listen port in B.conf

...
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name B.example.com;
    ...

then it works on B.example.com:8000

I went through many articles but did not able to achieve what I want. Maybe there is something I missed. I tried this several times on several servers manually and automated scripts but pulled by the same behavior.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there @itsmnthn,

The configuration looks good actually, it should be ok to run both Nginx server blocks on port 80.

What is the error that you get when you do that?

Also I could suggest checking your Nginx error log to see if there is some more information in there:

  • tail -100 /var/log/nginx/error.log

Feel free to share the output of the logs here as well after removing any sensitive information if any.

Regards,
Bobby