skip to Main Content

I’m trying to set up an nginx proxy pass between two docker containers. My frontend container seems to be working fine, but I’m getting a 502 error when trying to access the backend. I have two docker-compose files:

docker-compose.yml

version: "3.1"

services:
  backend:
    image: backend_image
    container_name: backend_container
  frontend:
    container_name: frontend_container
    image: frontend_image
  proxy:
    container_name: proxy_container
    image: nginx
    restart: unless-stopped
    ports:
      - "8080:80"
    depends_on:
      - website
      - frontend

docker-compose-development.yml

version: "3.1"

services:
  backend:
    container_name: backend_container_development
  frontend:
    build:
      context: ./site/public_html/frontend
      target: runner
    volumes:
      - ./site/public_html/frontend:/app
    command: npm run dev
    ports:
      - "3000:80"
    environment:
      NODE_ENV: development
  proxy:
    volumes:
      - ./nginx/local.conf:/etc/nginx/nginx.conf
      - ./logs:/var/logs

and my nginx local.conf file:

events {
    worker_connections 1024;
}

http {
    server {
        listen 80 default_server;

        location / {
            proxy_pass http://frontend:3000;
            error_log /var/log/frontend_errors.log;
        }

        location /api {
            proxy_pass http://backend:8000;
            error_log /var/log/backend_errors.log;
        }
    }
}

This is the error that I’m receiving

[error] 29#29: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.48.1, server: , request: "GET /api HTTP/1.1", upstream: "http://192.168.48.3:8000/api", host: "localhost:8080"

I’ve verified that the IP for the backend and frontend container is 192.168.48.3 and that the container run separately on localhost:8000 and localhost:3000

2

Answers


  1. The error message "connect() failed (111: Connection refused)" indicates that Nginx is unable to connect to the backend container at http://backend:8000. Here are some steps you can take to troubleshoot the issue:

    1. Check the backend container: Verify that the backend container is running by running docker ps. If it is not running, start it with docker-compose up backend.

    2. Verify the backend container’s port: Check that the backend container is listening on port 8000. You can do this by running docker-compose exec backend netstat -tlnp and verifying that port 8000 is in the list of open ports.

    3. Check network connectivity: Ensure that the frontend and backend containers can communicate with each other on the network. You can do this by running docker-compose exec frontend ping backend and docker-compose exec backend ping frontend.

    4. Verify the Nginx configuration: Ensure that the backend service is spelled correctly in the proxy_pass directive in the location /api block of the nginx.conf file. Also, check that the backend container is reachable from the proxy container by running docker-compose exec proxy ping backend.

    5. Check the backend container logs: Check the logs of the backend container for any error messages that may indicate why it is not responding. You can do this by running docker-compose logs backend.

    6. Verify the backend container’s IP address: Verify that the backend container is listening on the IP address that Nginx is trying to connect to. You can do this by running docker-compose exec backend netstat -tlnp | grep 8000.

    If none of these steps help, you may need to provide more information such as the contents of the Dockerfiles or any error messages in the frontend container logs.

    Login or Signup to reply.
  2. Got the same issue today, should use container`s "inner" port, in your case :80 instead :3000 and :8080:

    proxy_pass http://frontend:80;
    ...
    proxy_pass http://backend:80;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search