title: Docker Nginx Nodejs load balancing
body: <p>I have recently started to be interested in Docker and Nginx. I would like to set up load balancing but I don't know how to go about it. Can anyone tell me what I should do.
  I read a bit about upstream, but I don't really understand how to configure this part and connect it to the nginx configuration.</p>
  <pre><code>====docker-compose.yml=====
  version: &quot;3.7&quot;
    # expose:
    #   - 8080
    # ports:
    #   - 8080:8080
  
  services:
    nginx:
      image: nginx
      ports:
        - 8080:80
        - 443:443
      volumes:
        - ./.docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      restart: always
      depends_on:
        - backend
      networks:
        - nginx-network
    backend:
      container_name: backend
      restart: always
      command: npm run dev
      environment:
        - dbURI=mongodb://root:example@mongo:27017
      networks:
        - app-network
        - nginx-network
      depends_on:
        - mongo
      build:
        context: .
        dockerfile: ./.docker/Dockerfile
    mongo:
      container_name: mongo
      image: mongo
      restart: always
      networks:
        - app-network
      volumes:
        - mongo-data:/data/db
      environment:
        MONGO_INITDB_ROOT_USERNAME: root
        MONGO_INITDB_ROOT_PASSWORD: example
    mongo-express:
      container_name: mongo-express
      image: mongo-express
      restart: always
      ports:
        - 8081:8081
      networks:
        - app-network
      depends_on:
        - mongo
      environment:
        ME_CONFIG_MONGODB_ADMINUSERNAME: root
        ME_CONFIG_MONGODB_ADMINPASSWORD: example
        ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
  
  networks:
    app-network:
      driver: bridge
    nginx-network:
      driver: bridge
  
  volumes:
    mongo-data:
      driver: local
  </code></pre>
  <pre><code>=====Dockerfile=====
  FROM node:lts-alpine
  
  WORKDIR /app
  
  COPY package*.json ./
  
  RUN npm install
  
  COPY . .
  </code></pre>
  <pre><code>====default.conf nginx=====
  default.conf
  server {
      listen 80;
      listen [::]:80;
      server_name test.com;
  
      location /api {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-NginX-Proxy true;
          proxy_pass http://backend:8080;
          proxy_ssl_session_reuse off;
          proxy_set_header Host $http_host;
          proxy_cache_bypass $http_upgrade;
          proxy_redirect off;
      }
  }
  
  
  server {
      listen 443;
      listen [::]:443;
      server_name test.com;
  
      location /api {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-NginX-Proxy true;
          proxy_pass http://backend:8080;
          proxy_ssl_session_reuse off;
          proxy_set_header Host $http_host;
          proxy_cache_bypass $http_upgrade;
          proxy_redirect off;
      }
  }
  </code></pre>