Running multiple django projects using docker and nginx

Viewed 502

I am trying to do the following.

  • I have 3 django projects (NOT apps) (can be more).
  • Proj1: On port 8000, Proj2: 8001, and Proj3:8002

This is what I am trying to achieve:

User visits : localhost:8000

All urls under Pr1: Respond

User visits: localhost:8000/Pr2

All urls under Pr2: Respond

User visits: localhost:8000/Pr3

All urls under Pr3: Respond

Goal: Proj 1 is run with Docker and Nginx. Now I have 2 new projects to integrate. Every new sub project should be reached using only one main port (8000). But internally the request is directed towards the correct port of sub-project.

Why this goal: Achieve micro-service style structure. Sub projects (2 & 3) can be reached only after authentication through Proj1.

What have I tried: Honestly no clear idea where to start.

  • Added new projects to docker-compose...they all work when visited by their own port(localhost:8001/Prj2) ..

  • With nginx, tried adding a new server. No effect:

      server {
          listen 8001;
    
          location / {
              proxy_pass         http://web_socket:8080;
              proxy_redirect     off;
          }
      }
    
1 Answers

If you have "nginx" installed, so you can just create two files (site1.com.conf, site2.com.conf) in the following directory: "/etc/nginx/sites-enabled/" Then add inside this files codes below:
site1.com.conf

# the upstream component nginx needs to connect to
upstream site1 {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:49151; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name site1.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /var/www/site1.com/src/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /var/www/site1.com/src/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  site1;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

site2.com.conf

# the upstream component nginx needs to connect to
upstream site2 {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:49152; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name site2.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /var/www/site2.com/src/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /var/www/site2.com/src/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  site2;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

Then edit "/etc/hosts" and add there two lines:
127.0.0.1 site1.com
127.0.0.1 site2.com


After saving files, restart uwsgi (install if it is not installed) and **nginx**
**service uwsgi restart**
**sudo /etc/init.d/nginx restart**
**Note: all your django applications should be in the folder of "/var/www/site1.com/src" and "/var/www/site2.com/src"**
Related