0

I have simple docker-compose.yml where I would like to be able to use nginx as a proxy to the containers. For now I have two containers admin and api which later on I want to make talking to each other.

Right now with configuration presented below, when I try to access api.host.dev I'm getting this:

nginx-proxy     | nginx.1    | 2017/04/19 15:18:35 [error] 26#26: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.60.1, server: api.host.dev, request: "GET / HTTP/1.1", upstream: "http://172.18.0.4:9000/", host: "api.host.dev"
nginx-proxy     | nginx.1    | api.host.dev 192.168.60.1 - - [19/Apr/2017:15:18:35 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"

Right now I'm kind of out of ideas. Here is all configuration:

version: '2'
services:
    nginx-proxy:
       image: jwilder/nginx-proxy
       container_name: nginx-proxy
       ports:
        - "80:80"
        - "443:443"
       volumes:
        - /var/run/docker.sock:/tmp/docker.sock:ro

    admin:
        container_name: admin
        image: php:7.1-fpm
        restart: on-failure
        volumes:
            - ../admin:/var/www/admin
        working_dir: /var/www
        env_file:
            - ./variables/dev-admin.env

    api:
        container_name: api
        image: php:7.1-fpm
        restart: on-failure
        volumes:
            - ../api:/var/www/api
        working_dir: /var/www
        env_file:
            - ./variables/dev-api.env

Content of *.env files:

dev-api.env:

APP_ENV=DEV
VIRTUAL_HOST=api.host.dev
VIRTUAL_PORT=9000

dev-admin.env:

APP_ENV=DEV
VIRTUAL_HOST=admin.host.dev
VIRTUAL_PORT=9000

Content of /etc/nginx/conf.d/default.conf:

# admin.host.dev
upstream admin.host.dev {
                                ## Can be connect with "env_default" network
                        # admin
                        server 172.18.0.3:9000;
}
server {
        server_name admin.host.dev;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://admin.host.dev;
        }
}
# api.host.dev
upstream api.host.dev {
                                ## Can be connect with "env_default" network
                        # api
                        server 172.18.0.4:9000;
}
server {
        server_name api.host.dev;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://api.host.dev;
        }
}

Full output of docker-compose up:

sudo docker-compose up --remove-orphans
Recreating admin
Recreating nginx-proxy
Recreating api
Attaching to admin, api, nginx-proxy
admin    | [19-Apr-2017 15:18:24] NOTICE: fpm is running, pid 1
admin    | [19-Apr-2017 15:18:24] NOTICE: ready to handle connections
api      | [19-Apr-2017 15:18:24] NOTICE: fpm is running, pid 1
api      | [19-Apr-2017 15:18:24] NOTICE: ready to handle connections
nginx-proxy     | forego     | starting dockergen.1 on port 5000
nginx-proxy     | forego     | starting nginx.1 on port 5100
nginx-proxy     | dockergen.1 | 2017/04/19 15:18:25 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
nginx-proxy     | dockergen.1 | 2017/04/19 15:18:25 Running 'nginx -s reload'
nginx-proxy     | dockergen.1 | 2017/04/19 15:18:25 Watching docker events
nginx-proxy     | dockergen.1 | 2017/04/19 15:18:25 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
nginx-proxy     | nginx.1    | 2017/04/19 15:18:35 [error] 26#26: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.60.1, server: api.host.dev, request: "GET / HTTP/1.1", upstream: "http://172.18.0.4:9000/", host: "api.host.dev"
nginx-proxy     | nginx.1    | api.host.dev 192.168.60.1 - - [19/Apr/2017:15:18:35 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
nginx-proxy     | nginx.1    | 2017/04/19 15:18:45 [error] 26#26: *3 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.60.1, server: admin.host.dev, request: "GET / HTTP/1.1", upstream: "http://172.18.0.3:9000/", host: "admin.host.dev"
nginx-proxy     | nginx.1    | admin.host.dev 192.168.60.1 - - [19/Apr/2017:15:18:45 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
^[[A^[[Anginx-proxy     | nginx.1    | 2017/04/19 15:24:47 [error] 26#26: *5 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.60.1, server: api.host.dev, request: "GET / HTTP/1.1", upstream: "http://172.18.0.4:9000/", host: "api.host.dev"
nginx-proxy     | nginx.1    | api.host.dev 192.168.60.1 - - [19/Apr/2017:15:24:47 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
Tomasz
  • 4,847
  • 2
  • 32
  • 41

1 Answers1

1

When using compose, each service is exposed to the other containers using its service name, as if it were a DNS hostname. So you want to change references to e.g. admin.host.dev to just admin. For example, use this:

# admin.host.dev
upstream admin.host.dev {
                                ## Can be connect with "env_default" network
                        # admin
                        server admin:9000;
}

Notice in the server statement it now uses the hostname admin. This is automatically resolved to the container IP of your admin container.

(But note I didn't change the upstream's name - that is an internal name for nginx, and you don't necessarily need to change it.)

You would want to change the server name of the other upstream as well.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112