首页 > 解决方案 > Docker nginx connect() failed (113: Host is unreachable) while connecting to upstream

问题描述

I'm on a Macbook M1 and I have a problem with nginx (it looks like everything works fine for Ubuntu). I got "Host is unreachable" error in the console and "502 Bad Gateway" in the browser (localhost:8080 or localhost:8081). I'm using Docker 3.6.0

Any idea how to fix it?

my nginx.conf

worker_processes 4;

events { worker_connections 1024; }

http {

    sendfile on;
    client_max_body_size 25M;

    upstream docker-platform-ui {
        server platform-ui:8080;
    }

    upstream docker-web-platform {
        server web-platform:3000;
    }

    upstream docker-management-console-ui {
        server management-console-ui:5000;
    }

    upstream docker-document_console {
        server document-console:4000;
    }

    server {
        listen 8080;

        location ~ \/(admin*|api|webhook) {
          proxy_pass         http://docker-web-platform;
          proxy_redirect     off;
          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-Host $server_name;
        }

        location / {
            proxy_pass         http://docker-platform-ui/;
            proxy_redirect     off;
            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-Host $server_name;


            proxy_read_timeout 24h;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }

    server {
        listen 8081;

        location ~ \/(admin*|api|webhook) {
          proxy_pass         http://docker-web-platform;
          proxy_redirect     off;
          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-Host $server_name;

          proxy_read_timeout 24h;
          proxy_set_header Connection '';
          proxy_http_version 1.1;
          chunked_transfer_encoding off;
        }

        location / {
            proxy_pass         http://docker-management-console-ui/;
            proxy_redirect     off;
            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-Host $server_name;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}

I got the errors:

2021/09/14 19:06:04 [error] 35#35: *1 connect() failed (113: Host is unreachable) while connecting to upstream, client: 172.21.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://172.21.0.6:8080/", host: "localhost:8080"

reverse-proxy_1 | 172.21.0.1 - - [14/Sep/2021:19:06:04 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"

reverse-proxy_1 | 2021/09/14 19:06:07 [error] 35#35: *1 connect() failed (113: Host is unreachable) while connecting to upstream, client: 172.21.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.21.0.6:8080/favicon.ico", host: "localhost:8080", referrer: "http://localhost:8080/"

reverse-proxy_1 | 172.21.0.1 - - [14/Sep/2021:19:06:07 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"

EDIT: docker-compose.yml

version: '3'
services:
  ngrok:
    image: shkoliar/ngrok:latest
    ports:
      - 4551:4551
    links:
      - web-platform
    environment:
      - DOMAIN=web-platform
      - PORT=3000
  db:
    image: postgres:11.5
    ports:
      - "5432:5432"
    volumes:
      - ./tmp/db:/var/lib/postgresql/data

  redis:
    image: 'redis:alpine'
    volumes:
      - 'redis:/data'
    ports:
      - "6379:6379"

  mailcatcher:
    image: 'schickling/mailcatcher'
    ports:
      - "1080:1080"

  sidekiq:
    image: web-platform:latest
    depends_on:
      - redis
    volumes:
      - ../../web-platform:/web-platform
    command: sidekiq
    env_file:
      - web-platform/.env

  web-platform:
    image: web-platform:latest
    depends_on:
      - db
      - redis
    volumes:
      - ../../web-platform:/web-platform
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s"
    env_file:
      - web-platform/.env
    ports:
      - "3000:3000"
      - "8808:8808"
    tty: true
    stdin_open: true
  document-console:
    image: document-console:latest
    depends_on:
      - redis
    # need to remove volum for production mode, since Docker user operates with puppeteer and doesn't have permition to work with any directories
    # use volume only for nodemon for hot code reload in develop-local env
    volumes:
      - ../../document-console:/document-console
    command: yarn debug
    env_file:
      - document-console/.env
    ports:
      - "4000:4000"
      - "9229:9229"
    privileged: true

  # auditor:
  #   build:
  #     context: "../auditor"
  #   volumes:
  #     - ../auditor:/auditor
  #   command: ./run.sh

  platform-ui:
    image: platform-ui:latest
    volumes:
      - ../../platform-ui:/platform-ui
    command: yarn start
    env_file:
      - platform-ui/.env

  management-console-ui:
    image: management-console-ui:latest
    volumes:
      - ../../management-console-ui:/management-console-ui
    command: yarn start
    ports:
      - "5000:5000"
    env_file:
      - management-console-ui/.env

  reverse-proxy:
    image: reverse-proxy:latest
    depends_on:
      - platform-ui
      - management-console-ui
    ports:
      - "8080:8080"
      - "8081:8081"

  # qa-automation:
  #   image: qa-automation:latest
  #   volumes:
  #     - ../../qa-automation:/qa-automation

volumes:
  redis:
  postgres:

标签: macosdockernginx

解决方案


推荐阅读