Nginx代理(jwilder / nginx-proxy)通过对等方重置连接(502 Bad Gateway)

时间:2017-04-19 15:48:25

标签: nginx docker proxy docker-compose jwilder-nginx-proxy

我有简单的docker-compose.yml,我希望能够使用nginx作为容器的代理。现在我有两个容器adminapi,后来我想互相交谈。

现在有了下面的配置,当我尝试访问api.host.dev时,我得到了这个:

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"

现在我有点想法了。这是所有配置:

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

* .env文件的内容:

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

/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;
        }
}

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"

1 个答案:

答案 0 :(得分:1)

使用compose时,每个服务都使用其服务名称公开给其他容器,就像它是DNS主机名一样。所以你想改变对例如admin.host.dev只是admin。例如,使用此:

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

请注意,在server语句中,它现在使用主机名admin。这会自动解析为admin容器的容器IP。

(但请注意,我没有更改上游的名称 - 这是nginx的内部名称,您不一定需要更改它。)

您还希望更改其他上游的服务器名称。