Nginx – Connection Refused Error Nginx Nodejs

dockerdocker-composenginx

I am following this YouTube tutorial on how to configure Nginx Server on Docker but I am getting the following error

proxy_1 | 2018/07/14 22:18:24 [error] 5#5: *1 connect() failed
(111: Connection refused) while connecting to upstream, client:
172.19.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:49160/", host: "localhost"

/docker_compose.yml

version: '3'
services:
  nodecluster:
        build: nodecluster
        ports:
        - "49160:8000"
  proxy:
    build: proxy
    ports:
    - "80:80"  

nodecluster/Dockerfile

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8000
CMD [ "npm", "start" ]

proxy/Dockerfile

FROM nginx:alpine

RUN rm /etc/nginx/conf.d/*

COPY proxy.conf /etc/nginx/conf .d/

proxy/proxy.conf

listen 80;
server {
    location / {
        proxy_pass http://127.0.0.1:49160;
    }
}

enter image description here

I googled and looked through different forums for an answer no matter what I tried the connection refused error is still there. The
is up and running and if I access it directly using 127.0.0.1:49160 / localhost:49160 it works but it never get redirected from proxy to 127.0.0.1:49160 or like when I used http://nodecluster:49160/ or whatever.

Best Answer

Your nginx is trying to connect to localhost port 49160, but your nodecluster is running on another container, which has some other IP address.

You need to find the correct destination for nginx proxy_pass directive and fix the statement.