Nginx + Docker Compose - connect() failed (111: Connection refused) while connecting to upstream

Viewed 2710

thanks for taking the time to read this. I am trying to deploy my application to an AWS EC2 Instance using docker-compose. When i run the command docker-compose up and visit the site, I get an error from nginx saying the below error. I understand that nginx is receiving the request but is unable to find an upstream connection to my react app, and would appreciate any help in correctly configuring the ports/settings.

Error

2 connect() failed (111: Connection refused) while connecting to upstream, client: 108.212.77.70 server: example.com, request: "GET / HTTP/1.1", upstream: "http://172.29.0.4:8003/", host: "example.com"

Here is my nginx default config

upstream meetup_ws {
    server channels:8001;
}

upstream meetup_backend {
    server backend:8000;
}

upstream meetup_frontend {
    server frontend:8003;
}

server {
    listen 0.0.0.0:80;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com example.com;
    root /var/www/frontend;
    index index.html;
    ssl_certificate /etc/nginx/certs/fullchain.pem;
    ssl_certificate_key /etc/nginx/certs/privkey.pem;
    add_header Strict-Transport-Security "max-age=31536000";

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://meetup_frontend;
    }

    location /api {
        try_files $uri @proxy_api;
    }

    location @proxy_api {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://meetup_backend;
    }

    location /ws {
        try_files $uri @proxy_websocket;
    }

    location @proxy_websocket {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://meetup_ws;
    }
}

And this is my docker-compose.yml

version: '3'

services: 
    nginx:
        build: ./nginx
        restart: always
        ports:
            - 80:80
            - 443:443
        volumes:
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
            - ./frontend/build:/var/www/frontend
            - ./nginx/certs:/etc/nginx/certs
        depends_on: 
            - channels
    db:
        image: postgres:12.0-alpine
        ports: 
            - 5432:5432
        environment: 
            - POSTGRES_USER=postgres
            - POSTGRES_HOST=db
            - POSTGRES_PASSWORD=password
        volumes:
            - postgres_data:/var/lib/postgresql/data/
    backend: &backend
        build: ./backend
        volumes: 
            - ./backend:/app
        ports: 
            - 8000:8000
        command: ["python", "manage.py", "runserver"]
        env_file:
            - ./.env
        depends_on: 
            - db
            - redis
    frontend:
        build: ./frontend
        volumes: 
            - ./frontend:/app
            - node_modules:/app/node_modules
        ports: 
            - 8003:8003
        command: npm start
        stdin_open: true
    redis:
        image: "redis:5.0.7"
    worker_channels:
        <<: *backend
        command: ["python", "manage.py", "runworker", "channels"]
        depends_on:
            - db
            - redis
        ports:
            - 8002:8002
    channels:
        <<: *backend
        command: daphne -b 0.0.0.0 -p 8001 backend.asgi:application
        ports: 
            - 8001:8001
        depends_on:
            - db
            - redis

volumes:
    node_modules:
    postgres_data:
1 Answers

It is a bit embarrassing why the issue existed but I was able to solve the issue. I did ping frontend in my nginx container and it was successfully pinging the frontend container. Next I did curl -L http://frontend:8003 and it said curl: (7) Failed to connect to frontend port 8003: Connection refused. I went to the frontend container and did netstat -tulpn and it listed 3000 as the port that was exposed. I check my .env file and it was missing port=8003. Nginx was able to connect upstream afterwards.

Related