Docker Community Forums

Share and learn in the Docker community.

Nginx: (111: Connection refused) while connecting to upstream

Hi Team ,

I have issue with Ngnix configuration in docker container . I am unable to establish the connection from Ngnix to angular application in the same container .

Below is the docker file configuration:

FROM node:12.16.3-alpine as builder

RUN mkdir /app

WORKDIR /app

COPY package*.json ./

RUN npm install && npm install node-sass

COPY . .

RUN npm run build:ssr --prod --output-path=dist

EXPOSE 4200

CMD [ "node", "dist/server.js" ]

FROM nginx:alpine

COPY ./.nginx/nginx.conf /etc/nginx/nginx.conf

RUN rm -rf /usr/share/nginx/html/*

COPY --from=builder /app/dist /usr/share/nginx/html

EXPOSE 80 8080

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Ngnix configuration:


worker_processes 4;

events { worker_connections 1024; }

http {

server {
    listen 0.0.0.0:8080;
    listen [::]:8080;
    listen 127.0.0.1;
    server_name localhost;
    default_type application/octet-stream;

    gzip                    on;
    gzip_comp_level         6;
    gzip_vary               on;
    gzip_min_length         1000;
    gzip_proxied            any;
    gzip_types              text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers            16 8k;
    gunzip on;
    client_max_body_size    256M;
    
    root /usr/share/nginx/html;
    autoindex on;


    location / {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
		proxy_pass http://127.0.0.1:4200;
        }
  }
}

Below is the error which I am getting in this case

2021/03/14 12:41:24 [error] 8#8: *5 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4200/", host: "localhost:8080"

172.17.0.1 - - [14/Mar/2021:12:41:24 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"

172.17.0.1 - - [14/Mar/2021:12:41:26 +0000] "\x05\x02\x00\x01" 400 157 "-" "-"

It would be of great help if any thing is missing in my configuration . Any help in this aspect would be really helpful and appreciated

Thanks
Uday

Hi @testtriangle
did you find a solution for this problem? I am also facing a similar problem when proxied my prometheus container with nginx container.
The problem persists when I launch the nginx as a standalone docker container as well as in kubernetes.
Thanks.

Good morning,

one thing I can spot is that you have a two-staged-build-process where you add a CMD and a EXPOSE to it (which IMHO are useless).
Afterwards you copy some content from this builder-image to your productive image. That seems fine.
But in your productive image you only have the ENTRYPOINT to start NginX, not your NodeJS-application… I guess the reason for NginX not forwarding requests to the NodeJS-application in the same container (so the 127.0.0.1 is OK) is that the NodeJS-application is not running… :slight_smile:
So ensure that both services (NginX + your NodeJS-application) are started in the resulting container.