Nginx returns 502 error when using with docker-compose

Published

I am trying to create a reverse-proxy for my NestJS API and Angular app. I am running everything in docker containers, using docker-compose. For some reason I can not access to my running container through nginx.

I always get this error:

[error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4200/", 
host: "localhost"

This is my nginx-config:

events {}
http {
    server {
        listen 80;
        server_name localhost;

        location / {
            proxy_pass http://localhost:4200/;
        }

        location /api/ {
            proxy_pass http://localhost:3000/api;
        }
    }
}

And this is my docker-compose:

version: "3"
services:
  webserver:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    networks:
      - bunteto
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf

  backend:
    build:
      context: ./project-backend
      dockerfile: stage.Dockerfile
    env_file:
      - ./project-backend/environments/stage.env
    volumes:
      - ./project-backend/src:/usr/src/app/src
    ports:
      - "3000:3000"
    networks:
      - project

  frontend:
    build:
      context: ./project-frontend
      dockerfile: stage.Dockerfile
    ports:
      - "4200:4200"
    networks:
      - bunteto

networks:
  bunteto:

If I try to access the containers with: localhost:3000 and localhost:4200 they are fine. Altough when I am trying with 127.0.0.1 instead of localhost they are not fine.

Source: Docker Questions

Published
Categorised as docker, nginx Tagged ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq