Question

How to host 2 Django Application using gunicorn & nginx in Production

Posted July 13, 2021 153 views
NginxDjangoPython Frameworks

Hello
I want to host 2 django website in a same server and with 2 domain is it possible to host 2 website in a same server if yes so please tell me how can i will do this

1. First Django website
 file location --> /home/user/site1
 file of env --> /home/user/site1/env
 my domain name --> site1.com
 file location of wsgi --> /home/user/site1/site1/wsgi.py
2. Second Django website
 file location --> /home/user/site2
 file to env --> /home/user/site2/env
 file location of wsgi --> /home/user/site2/site2/wsgi.py
 my domain name --> site2.com

If you want more information please tell me I will give you and please tell me how to host these 2 django website please 🙏🙏

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 @harshku2630,

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 Erin Glass
Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. In this guide, we'll discuss how to get Nginx installed on your Ubuntu 20.04 server.