0

I have 2 different docker-compose YML files in 2 suppurate directories. They are both on the same server and on the same Docker network. My containers with application is in the first YML file and the second YML file has only one Nginx container that proxies requests to the first.

first YML file

version: '3'

services:

  chatweb:
   image: nginx:latest
   restart: always
   volumes:
   - ./angular_dist:/code
   - ./site_prod.conf:/etc/nginx/conf.d/site.conf

   networks:
   - my-net

  chatmongodb:
    image: mongo:5.0
    restart: always
    volumes:
      - ./mongodb_data_container:/data/db
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

    environment:
      - MONGO_INITDB_ROOT_USERNAME=test 
      - MONGO_INITDB_ROOT_PASSWORD=testpass
      - MONGO_INITDB_DATABASE=testdb

    networks:
      - my-net

  chatnode:
    build: 
      dockerfile: chat_node_dev
      context: .
    command: npm run server
    restart: always
    volumes:
      - ./nodejs:/usr/app/
    ports:
      - "5004:5001"
    depends_on:
      - chatmongodb
    
    networks:
      - my-net

  chatnodesocketio:
    build: 
      dockerfile: chat_node_socketio_dev
      context: .
    restart: always
    volumes:
      - ./node_socketio:/usr/app/
      - ./nodejs/models:/usr/app/models
      - ./nodejs/config:/usr/app/nodejs_config
    depends_on:
      - chatmongodb

    networks:
      - my-net


  mailhog:
    image: mailhog/mailhog
    restart: always
    logging:
      driver: 'none'  # disable saving logs
    ports:
      - 1025:1025 # smtp server
      - 8025:8025 # web ui


networks:
  my-net:
      external: true

The second YML file has Nginx proxy server

version: '3'

services:

  nginx:
   image: nginx:latest
   restart: always
   ports:
   - "80:80"
   volumes:
   - ./site_prod.conf:/etc/nginx/conf.d/site.conf

   networks:
   - my-net


networks:
  my-net:
      external: true

and here is the nginx site_prod.conf for second YML

server {
    index index.html;
    server_name myserver.com;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;


     location /{
     proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://chatweb:80;

    }

 
}

server {
    server_name socket.myserver.com;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
        location / {
                proxy_pass http://chatnodesocketio:3000;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_redirect off;
                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;
        }

 
}

server {
    server_name api.myserver.com;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
   
         location /{
         proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://chatnode:5001;

        }

 
}

Everything works well but when i restart the containers that is in the first YML files i get 502 error in the Nginx proxy that is started by the second YML file.

Here is the error in the logs

2022/07/29 06:32:19 [error] 23#23: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 79.180.48.255, server: api.myserver.com, request: "GET / HTTP/1.1", upstream: "http://172.18.0.5:5001/", host: "api.myserver.com"

Please help me: what do i need to change in my current setup to be able to restart my application that is in the first YML file without restarting the server that proxies that is described in the second YML file.

edited this answer seems to be a correct solution https://serverfault.com/a/916786/363583 this solution solve my problem by disabling caching for nginx upstreams

user2265529
  • 133
  • 1
  • 5

0 Answers0