Docker Angular application with Nginx: (111: Connection refused) while connecting to upstream

Viewed 438

I'm trying to use Nginx for a reverse proxy. I have an Angular application running on Node server on port 4200. I am using a Docker image for the deployment

Below is the Dockerfile 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;"]

I have the Nginx configuration as below:

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;
        }
  }
}

When I am running the image in the Docker container using the command

docker run --name application -d -p 8080:8080 app 

I am getting the below error:

*5 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xx.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4200/", host: "localhost:8080" xxx.xxx.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"
0 Answers
Related