0

I am currently stuck on making nginx proxy to the node load balancer. It gives the following error when making a request on 185.146.87.32:5000/:

2020/06/01 13:23:09 [warn] 6#6: *1 upstream server temporarily disabled while connecting to upstream, client: 86.125.198.83, server: domain.ro, request: "GET / HTTP/1.1", upstream: "http://185.146.87.32:5002/", host: "185.146.87.32:5000"

I managed to make this work on a local system, but now I am trying to make it work on a remote server. BACKEND_SERVER_PORT_1=5001 BACKEND_SERVER_PORT_2=5002 BACKEND_NODE_PORT=5000 BACKEND_NGINX_PORT=80 CLIENT_SERVER_PORT=3000 ADMIN_SERVER_PORT=3006 NGINX_SERVER_PORT=80 API_HOST="http://domain.ro"

This is the docker-compose:

version: '3'
services:

#####################################
#   Setup for NGINX container
#####################################
nginx:
  container_name: domain_back_nginx
  build:
    context: ./nginx
    dockerfile: Dockerfile
  image: domain/domain_back_nginx
  ports:
    - ${BACKEND_NODE_PORT}:${BACKEND_NGINX_PORT}
  volumes:
    - ./:/usr/src/domain
  restart: always
#####################################
#   Setup for backend container
#####################################
backend_1:
  container_name: domain_back_server_1
  build:
    context: ./
    dockerfile: Dockerfile
  image: domain/domain_back_server_1
  ports:
    - ${BACKEND_SERVER_PORT_1}:${BACKEND_NODE_PORT}
  volumes:
    - ./:/usr/src/domain
  restart: always
  command: npm start
#####################################
#   Setup for backend container
#####################################
backend_2:
  container_name: domain_back_server_2
  build:
    context: ./
    dockerfile: Dockerfile
  image: domain/domain_back_server_2
  ports:
    - ${BACKEND_SERVER_PORT_2}:${BACKEND_NODE_PORT}
  volumes:
    - ./:/usr/src/domain
  restart: always
  command: npm start

The Dockerfile for node is:

FROM node:12.17.0-alpine3.9

RUN mkdir -p /usr/src/domain

ENV NODE_ENV=production

WORKDIR /usr/src/domain

COPY package*.json ./

RUN npm install --silent

COPY . .

EXPOSE 5000

The config file for nginx is:

upstream domain {
  least_conn;
  server backend_1 weight=1;
  server backend_2 weight=1;
}

server {
   listen 80;
   listen [::]:80;

   root /var/www/domain_app;
   server_name domain.ro www.domain.ro;
   location / {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://domain;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

The Dockerfile for nginx is:

FROM nginx:1.17-alpine as build

#!/bin/sh

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

COPY default.conf /etc/nginx/conf.d

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

1 Answer 1

1

don't expose your backend to world,

create a docker network for your services, then expose nginx, it's the best practice, but in your case you didnt specify backend ports in nginx.conf

upstream domain {
  least_conn;
  server backend_1:5000 weight=1;
  server backend_2:5000 weight=1;
}

you should do below:

version: '3'
services:

#####################################
#   Setup for NGINX container
#####################################
nginx:
  container_name: domain_back_nginx
  build:
    context: ./nginx
    dockerfile: Dockerfile
  image: domain/domain_back_nginx
  networks:
    - proxy
  ports:
    - 5000:80
  volumes:
    - ./:/usr/src/domain
  restart: always
#####################################
#   Setup for backend container
#####################################
backend_1:
  container_name: domain_back_server_1
  build:
    context: ./
    dockerfile: Dockerfile
  image: domain/domain_back_server_1
  networks:
    - proxy

  ## always expose, just in case you missed it in Dockerfile, this will expose the port(s)
  ## just in defined networks
  expose:
    - 5000
  volumes:
    - ./:/usr/src/domain
  restart: always
  command: npm start
#####################################
#   Setup for backend container
#####################################
backend_2:
  container_name: domain_back_server_2
  build:
    context: ./
    dockerfile: Dockerfile
  image: domain/domain_back_server_2
  networks:
    - proxy

  ## always expose, just in case you missed it in Dockerfile, this will expose the port(s)
  ## just in defined networks
  expose:
    - 5000
  volumes:
    - ./:/usr/src/domain
  restart: always
  command: npm start

networks:
  proxy:
    external:
      name: proxy

but after all, i recommend jwilder/nginx-proxy

Sign up to request clarification or add additional context in comments.

1 Comment

I did have a look at jwilder/nginx-proxy before, but I don't really get how it should work. Especially, I am confused on this part: -v /var/run/docker.sock:/tmp/docker.sock:ro. What are those files and what should they contain? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.