Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Hello I want to host 2 djagno websites using Gunicorn and Nginx and I don't know how to do this this is my first time to host 2 django websites in one server and 2 domain so please tell me how to host 2 django website. Here is my 1 file located /var/www/site1 and here is my 2 file /var/www/site2

like image 336
Harsh kumar Avatar asked Nov 15 '25 12:11

Harsh kumar


1 Answers

Assuming you run your gunicorn instances on ports 8081 and 8082, you have two options:

Option 1: Subdomains

To use subdomains to reverse-proxy with NGINX, see this answer.

http://site1.example.com/ redirects to Site 1 and http://site2.example.com/ to Site 2.

NGINX config:

server {
    listen 80;
    server_name site1.example.com;
    location / {
        proxy_pass http://127.0.0.1:8082;
    }
}
server {
    listen 80;
    server_name site2.example.com;
    location / {
        proxy_pass http://127.0.0.1:8082;
    }
}

Option 2: Locations

http://example.com/site1/ now redirects to Site 1 and http://example.com/site2/ to Site 2.

NGINX config:

server {
    listen 80;
    server_name example.com;
    location /site1/ {
        proxy_pass http://127.0.0.1:8081;
    }
    location /site2/ {
        proxy_pass http://127.0.0.1:8082;
    }
}

EDIT - Option 3: Domains

You can also just use different domains for the different sites just as in option 1.

http://site1.com/ redirects to Site 1 and http://site2.com/ to Site 2.

NGINX config:

server {
    listen 80;
    server_name site1.com;
    location / {
        proxy_pass http://127.0.0.1:8082;
    }
}
server {
    listen 80;
    server_name site2.com;
    location / {
        proxy_pass http://127.0.0.1:8082;
    }
}

Note: These are bare minimum setups and may need additional configuration to work with your specific services.

like image 110
xle Avatar answered Nov 17 '25 08:11

xle