'Connection Refused while connecting to upstream (nginx + uwsgi + django)

I am trying to create a multi-container setup with an Nginx reverse proxy in front of the Django(with uwsgi) apps - auth and shortner

I am getting the following error when I am trying to do GET 0.0.0.0:80/auth/ -

docker_djangoapps_nginx-nginx-1     | 2022/05/08 15:56:06 [error] 23#23: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.176.1, server: , request: "GET /auth/ HTTP/1.1", upstream: "uwsgi://0.0.0.0:8080", host: "0.0.0.0:80"
docker_djangoapps_nginx-nginx-1     | 192.168.176.1 - - [08/May/2022:15:56:06 +0000] "GET /auth/ HTTP/1.1" 502 157 "-" "PostmanRuntime/7.29.0" "-"

I am unable to understand why uwsgi is giving a connection refused when nginx is trying to connect upstream?

My project directory looks like this -

.
├── README.md
├── auth
│   ├── Dockerfile
│   ├── app_auth
│   │   ├── __init__.py
│   │   ├── apps.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── manage.py
│   ├── requirements.txt
│   └── service
│       ├── __init__.py
│       ├── asgi.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
├── docker-compose.yaml
├── nginx
│   ├── Dockerfile
│   └── nginx.conf
└── shortner
    ├── Dockerfile
    ├── app_shortner
    │   ├── __init__.py
    │   ├── admin.py
    │   ├── apps.py
    │   ├── models.py
    │   ├── tests.py
    │   ├── urls.py
    │   └── views.py
    ├── manage.py
    ├── requirements.txt
    └── service
        ├── __init__.py
        ├── asgi.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

My docker-compose.yml file for the project is -

version: '3.8'

services:
  auth:
    build: ./auth
    command: uwsgi --http :8080 --module=service.wsgi:application
    #command: [sh, -c, "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"]
    expose:
      - 8080
    env_file:
      - ./.env.dev

  shortner:
    build: ./shortner
    command: uwsgi --http :8081 --module=service.wsgi:application
    #[sh, -c, "python manage.py makemigrations && python manage.py migrate --run-syncdb && python manage.py runserver 0.0.0.0:8000"]
    expose:
      - 8081
    env_file:
      - ./.env.dev
  
  nginx:
    build: ./nginx
    ports:
      - 80:80
    depends_on:
      - auth
      - shortner

DockerFile for Nginx reverse proxy -

FROM nginx:stable-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/

This is the nginx.conf file (in the above Dockerfile) -

server {
    listen       0.0.0.0:80;
    location /auth/ {
        uwsgi_pass 0.0.0.0:8080;
        include /etc/nginx/uwsgi_params;
    }
    location /short/ {
        uwsgi_pass 0.0.0.0:8081;
        include /etc/nginx/uwsgi_params;
    }
}

Dockerfile for my Django apps(both) is -

FROM python:3.10-alpine
RUN apk add build-base
RUN apk add linux-headers

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

COPY . .

Environment file for the django apps (as referenced in the docker-compose.yml) has the following line -

DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] 0.0.0.0


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source