Question

Unable to connect Nginx and backend container in my droplet

hello. I am running my web app constituted of

  • nginx (installed directly in the droplet)
  • django container (backend)
  • Next.js container (frontend0. I deploy all in the droplet I bought a domain name and pointed it to my IP server adress.

My app is running well locally, After deployement, I am getting the famous 502 bad gateway. i have the following files /etc/nginx/sites-available/web.conf


###########################################  UPSTREAMS

#upstream django_upstream {
#  server django:8000;
#}


#upstream nextjs_upstream {
#  server frontend:3000;
#}

############################################  SERVER

server {
    listen 80;
    server_name domain.com www.domain.com.ca;
    #server_tokens off;

    location / {
       try_files $uri @proxy_frontend;
    }

    location @proxy_frontend {
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://localhost:3000;
        #proxy_pass   http://nextjs_upstream; <--- (tried this with upstrea. but not worked)
    }

    
    #----------------------      Backend      ------------------------------


    location /backend/api {
        try_files $uri @proxy_api;
    }

    location /backend/api/docs {
        try_files $uri @proxy_api;
    }

    location /admin {
        try_files $uri @proxy_api;
    }

    location @proxy_api {
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://localhost:8000;
        # proxy_pass   http://django_upstream; <--- (tried this with upstrea. but not worked)
    }

    location /django_static/ {
        alias /app/backend/staticfiles/;
    }

    location /django_media/ {
        alias /app/backend/mediafiles/;
    }


}

and my docker-compose file is

version: '3.9'

services:
  db:
    build:
      context: .
      dockerfile: ./docker/database/Dockerfile
    container_name: c-db
    restart: always
    volumes:
      - db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=${DB_NAME}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASS}

  django:
    hostname: django
    restart: always
    image: i-api-prod:django-prod
    container_name: c-api
    build:
      context: .
      dockerfile: ./docker/backend/Dockerfile
    volumes:
      - staticfiles:/app/backend/staticfiles
      - mediafiles:/app/backend/mediafiles
    environment:
      - DB_HOST=db
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASS=${DB_PASS}
      - SECRET_KEY=${DJANGO_SECRET_KEY}
      - ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS}
    expose:
      - 8000
    depends_on:
      - db
    # networks:
    #   - django-network

  frontend:
    hostname: frontend
    build:
      context: .
      dockerfile: ./docker/frontend/Dockerfile
    container_name: c-frontend
    expose:
      - 3000

    # volumes:
    #   - frontend_static_assets:/app/frontend/static_assets
    depends_on:
      - django

volumes:
  db:
  staticfiles:
  mediafiles:

The .env file for my project is


# Database variables

DB_NAME=dbname
DB_USER=rootuser
DB_PASS=changeme



DJANGO_SECRET_KEY=secretkey_for_production_environment


DJANGO_ALLOWED_HOSTS=localhost 0.0.0.0 


# Frontend variables

API_BASE_URL="http:0.0.0.0/backend"

When I check nginx status is active(running).

  • When I tried to a request in the broweser and check the logs, I obtained the following error.
2022/12/31 13:44:51 [error] 42558#42558: *62 connect() failed (111: Unknown error) while connecting to upstream, client: 31.2.136.184, server: domain.com, request: "GET /2.FKbxrpmVH1CttsoTtQEudY?O8A=bMnAfQJ7939h5tlyVuH_Uog&uS=ulh.AJJYMMaSJctgUQWEwiIZ.D&RPFsTVxAu=XkD HTTP/1.1", upstream: "http://localhost:3000/2.FKbxrpmVH1CttsoTtQEudY?O8A=bMnAfQJ7939h5tlyVuH_Uog&uS=ulh.AJJYMMaSJctgUQWEwiIZ.D&RPFsTVxAu=XkD", host: "www.ws.k12.ny.us"

and the understand that the problem is not nginx, but the connection to my backend and frontend. But I don’t know how to fix it.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
January 3, 2023

Hi there,

Indeed the Nginx configuration looks good. The error indicates that the Docker container that is supposed to be running on 3000 is not reachable.

If you check the container status with docker ps -a is the container up and running?

Also if you check the logs of that container do you see any errors in there?

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

New accounts only. By submitting your email you agree to our Privacy Policy.

© 2023 DigitalOcean, LLC.