How Can i have multiple Django projects in a droplet?

September 30, 2015 4.7k views
Django Nginx DNS Python Ubuntu

Hi i am triying to intall 3 Projects in my droplet with nginx and gunicorn, i have 2 projects created with django and one with only html (a static website), i could install the static website listen the 80 port, also, i could put one django project but listen 8082 port, because when i put it to listen the 80 port only i could see the static website, so my question is, i have to change the port each time i going to install a new project or web site? and how i can install 2 django projects In a same droplet ?

1 Answer

In order to have more than one Django app listening on port 80, you'll need to allow Nginx to direct requests to the correct app. You'll also need to set up DNS records. By setting the server_name directive in each of your server blocks, Nginx will know what to serve.

Here's a simplified example:

upstream app_server_one {
    server 127.0.0.1:9000 fail_timeout=0;
}

upstream app_server_two {
    server 127.0.0.1:7000 fail_timeout=0;
}

server {
    listen 80 default_server;
    server_name: example.com;
    root /var/www//html;
    index index.html index.htm;

   # [snip...]
}

server {
    listen 80;
    server_name app1.example.com;

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_one;
    }
}

server {
    listen 80;
    server_name app2.example.com;

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_two;
    }
}

In the example, requests to example.com will serve static content from /var/www//html One to app1.example.com will serve a Django app listening on port 9000 and app2.example.com will serve the app listening on port 7000.

You can get more info on how to use Nginx server block in this tutorial:

And on using Django with Nginx:

by Justin Ellingwood
Nginx is an incredibly powerful web server and reverse proxy. In this guide, we'll discuss how to set up server blocks on Ubuntu 14.04 in order to server multiple domains from a single server.
  • Wow! thanks a lot for the answer, I will try this.

  • @asb I try this but in the 80 port I only can see the principal site, the default server, so how can i see the others sites?

    • @jrobertosampayo Could you post your Nginx configuration on a pastebin or provide any more details of your set up?

      • This is my nginx configuration http://pastebin.com/jU4E3nBe where I have 1 html page that it works and other one that is a django project but it doesnt work, you can see the first page on 45.55.137.117 or www.viveresparasoca.com.ve

      • This is another nginx configuration (other file) http://pastebin.com/2hS5BPSE in this configuration i have the django project listen the 8082 port, is the solution that i found to see the project but the static files listen 8000 port, so i had to set the static files manually listen the 8000 port like this (<link rel="stylesheet" href="http://45.55.137.117:8000/static/css/bootstrap.css">) and here is my problem, because i want to use the django tag {% static "css/bootstrap.css" %} but if i use this tag the static files doesnt appear. You can see this configuration here http://45.55.137.117:8082

      • @asb I would appreciate everything you could do for me

        • It seems like you might have some issues with your DNS records. Are these domains that you have registered and set up DNS records for? Pointing multiple apps to port 80 will only work if you are using DNS.

  • Thanks @asb I could solve the problem, I just register the other domain cuadratudia.com.ve, configure the records and change a bit my nginx configuration, now the file is this:

    upstream django_server2{
        server 127.0.0.1:9500 fail_timeout=0;
    }
    
    # HTML SERVER VIVERESPARAISOCA.COM
    server {
        listen 80;
    
        root /var/www/viveresparaiso.com/public_html;
        index index.html index.htm;
    
        # Make site accessible from http://localhost/
        server_name viveresparaisoca.com.ve www.viveresparaisoca.com.ve;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
        }
    
    
    }
    # DJANGO SERVER CUADRATUDIA.COM
    server {
        listen 80;
    
            root /home/django/discotecas;
            index /discotecas/wsgi.py;
    
            access_log /home/django/logs/access.log;
    
        client_max_body_size 4G;
        server_name cuadratudia.com.ve www.cuadratudia.com.ve;
    
        keepalive_timeout 5;
    
            location /static/ {
                #autoindex on;
                #alias /home/django/discotecas/static/;
                root /home/django/discotecas/;
            }
    
            # Proxy the static assests for the Django Admin panel
        location /static/admin {
                alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
        }
    
        location / {    
    
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_pass http://django_server2;       
        }
    }
    
Have another answer? Share your knowledge.