Nginx giving 502 Bad Gateway

I'm following this tutorial (with this repository) to deploy my Django project. I added nginx configuration as described, with all the files and directories seeming to match. The project I'm trying to deploy also has other dependencies like celery or selenium, but those work okay with docker-compose. When I run docker-compose up the app seems to start without errors (celery tasks are executed, etc.), and the proxy gives this log:

suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: using the "epoll" event method
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: nginx/1.23.3
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4) 
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: OS: Linux 5.15.49-linuxkit
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: getrlimit(RLIMIT_NOFILE): 1048576:1048576
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: start worker processes
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: start worker process 9
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: start worker process 10
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: start worker process 11
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: start worker process 12
suii-proxy-1        | 2023/01/26 08:43:00 [notice] 8#8: start worker process 13

But when trying to access to http://127.0.0.1 I get a 502 Bad Gateway like this:

enter image description here

With this log error:

suii-proxy-1        | 2023/01/27 07:10:28 [error] 11#11: *28 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://172.18.0.6:9000", host: "127.0.0.1", referrer: "http://127.0.0.1/"
suii-proxy-1        | 172.18.0.1 - - [27/Jan/2023:07:10:28 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://127.0.0.1/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "-"

I'm new to nginx and wsgi, but I wonder if the upstream and/or client are right. These are the files I'm using for reference. This is proxy/default.conf.tpl:

server {
    listen ${LISTEN_PORT};

    location /static {
        alias /vol/static;
    }

    location / {
        uwsgi_pass              ${APP_HOST}:${APP_PORT};
        include                 /etc/nginx/uwsgi_params;
        client_max_body_size    10M;
    }
}

And the proxy/Dockerfile:

FROM nginxinc/nginx-unprivileged:1-alpine
LABEL maintainer="londonappdeveloper.com"

COPY ./default.conf.tpl /etc/nginx/default.conf.tpl
COPY ./uwsgi_params /etc/nginx/uwsgi_params
COPY ./run.sh /run.sh

ENV LISTEN_PORT=8000
ENV APP_HOST=app
ENV APP_PORT=9000

USER root

RUN mkdir -p /vol/static && \
    chmod 755 /vol/static && \
    touch /etc/nginx/conf.d/default.conf && \
    chown nginx:nginx /etc/nginx/conf.d/default.conf && \
    chmod +x /run.sh

VOLUME /vol/static

USER nginx

CMD ["/run.sh"]

Those are proxy/run.sh:

#!/bin/sh

set -e

envsubst < /etc/nginx/default.conf.tpl > /etc/nginx/conf.d/default.conf
nginx -g 'daemon off;'

And the scripts/run.sh:

#!/bin/sh

set -e

python manage.py wait_for_db
python manage.py collectstatic --noinput
python manage.py migrate
celery -A suii worker -l INFO
celery -A suii beat -l INFO

uwsgi --socket :9000 --workers 4 --master --enable-threads --module suii.wsgi

And also a script of the docker-compose-deploy.yml:

version: '3.9'

services:
  app:
    build:
      context: .
    restart: always
    volumes:
      - static-data:/vol/web
    environment:
      - DB_HOST=db
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASS=${DB_PASS}
      - SECRET_KEY=${SECRET_KEY} 
      - ALLOWED_HOSTS=${ALLOWED_HOSTS}
      - CELERY_BROKER=${CELERY_BROKER}
      - CELERY_BACKEND=%{CELERY_BACKEND}
    depends_on:
      - db
    
  db:
    image: postgres
    restart: always
    volumes:
      - postgres-data:/var/lib/postgresql/data
    environment:
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASS=${DB_PASS}
      - TZ=${TZ}
    
  proxy:
    build:
      context: ./proxy
    restart: always
    depends_on:
      - app
    ports:
      - 80:8000
    volumes:
      - static-data:/vol/static

As it seems the nginx server is pointing to a wrong direction, where is the error that prevents it to point to the django app?

This is also how my Docker desktop looks if helpful:

enter image description here

I think that you have to add a port after http://127.0.0.1

Back to Top