Search code examples
dockernginxdocker-swarm

Cannot Connect Container on Overlay Network: [emerg] invalid host


I am trying to reverse proxy a request through an nginx container in a swarm to a standalone container which shares the same overlay network.

tldr; I receive the following error:

2018/03/15 19:00:35 [emerg] 1#1: invalid host in upstream "http://nginx" in /etc/nginx/nginx.conf:96
nginx: [emerg] invalid host in upstream "http://nginx" in /etc/nginx/nginx.conf:96

The standalone container contains an app which has another nginx frontend:

version: "3"
services:
  nginx:
    restart: always
    container_name: my.nginx
    build: ./nginx
    networks:
      - default
      - my-overlay-network
    depends_on:
      - another-service

... other services

networks:
   my-overlay-network:
     external: true

I start this app with docker-compose up -d.

My swarm contains the reverse proxy:

version: "3"
services:
  reverseproxy:
    build: ./reverseproxy
    image: reverse_proxy
    networks:
      - my-overlay-network
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt
    deploy:
      replicas: 10
      restart_policy:
        condition: on-failure

    networks:
      my-overlay-network:
        external: true

If I startup the nginx swarm without specifying a proxy_pass for the standalone app, I can successfully ping the other host like so:

ping http://nginx/

I can confirm the other host receives this request based on the nginx logs.

However, if I specify the docker standalone app in the reverse proxy:

upstream standalone {
    server http://nginx/;
}

and

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    ... other stuff ...

    location / {
        proxy_pass http://standalone/;
    }
}

I get the following errors:

2018/03/15 19:00:35 [emerg] 1#1: invalid host in upstream "http://nginx" in /etc/nginx/nginx.conf:96
nginx: [emerg] invalid host in upstream "http://nginx" in /etc/nginx/nginx.conf:96

Solution

  • The issue is that the syntax for upstream is incorrect: it takes a host and port, so it should be:

    upstream standalone {
        server nginx;
    }