[英]Nginx proxy (jwilder/nginx-proxy) Connection reset by peer (502 Bad Gateway)
I have simple docker-compose.yml
where I would like to be able to use nginx
as a proxy to the containers. 我有一个简单docker-compose.yml
,我想在其中使用nginx
作为容器的代理。 For now I have two containers admin
and api
which later on I want to make talking to each other. 现在,我有两个容器admin
和api
,以后我想互相交谈。
Right now with configuration presented below, when I try to access api.host.dev
I'm getting this: 现在,使用下面显示的配置,当我尝试访问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"
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: * .env文件的内容:
dev-api.env: dev-api.env:
APP_ENV=DEV
VIRTUAL_HOST=api.host.dev
VIRTUAL_PORT=9000
dev-admin.env: dev-admin.env:
APP_ENV=DEV
VIRTUAL_HOST=admin.host.dev
VIRTUAL_PORT=9000
Content of /etc/nginx/conf.d/default.conf
: /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
: 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"
When using compose, each service is exposed to the other containers using its service name, as if it were a DNS hostname. 使用compose时,每个服务都会使用其服务名称公开给其他容器,就好像它是DNS主机名一样。 So you want to change references to eg admin.host.dev
to just admin
. 因此,您想admin.host.dev
引用更改为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
. 注意,在server
语句中,它现在使用主机名admin
。 This is automatically resolved to the container IP of your admin
container. 这将自动解析为您的admin
容器的容器IP。
(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.) (但是请注意,我没有更改上游的名称-这是nginx的内部名称,您不一定需要更改它。)
You would want to change the server name of the other upstream as well. 您还希望更改另一个上游的服务器名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.