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 ?

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.

×
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?

  • 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;       
        }
    }
    
Submit an Answer