我希望能够访问包含Web应用程序的同一主机上的多个容器。
当我想访问主机(通过他的IP地址)或容器(例如通过host_ip_adress / container1)时,我从Nginx获得了503错误。我想要的是通过ip_addrress_host / container1访问我的container1。
我在互联网上找到的解决方案是设置一个nginx-proxy前端服务器(来源:https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker/)
我的docker-compose文件:
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock
site_a:
image: php:7.0-apache
expose:
- "80"
environment:
- VIRTUAL_HOST=192.168.12.28/container1
- VIRTUAL_PORT=80
volumes:
- ./php:/var/www/html
site_b:
image: php:7.0-apache
expose:
- "80"
environment:
- VIRTUAL_HOST=192.168.12.28/container2
- VIRTUAL_PORT=80
volumes:
- ./php:/var/www/html
我使用以下命令运行它:
docker-compose up
我在 / etc / hosts文件中的条目:
127.0.1.1 container1
127.0.0.1 container2
我从外面提出请求时看到的日志:
nginx-proxy_1 | nginx.1 | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /container1 HTTP/1.1" 503 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
nginx-proxy_1 | nginx.1 | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /favicon.ico HTTP/1.1" 503 615 "http://192.168.12.28/container1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
感谢您的帮助,抱歉我的英语不好! : - )
修改: 我试图将nginx-proxy容器和我的Web应用程序容器放在与docker hub(https://hub.docker.com/r/jwilder/nginx-proxy/)相同的“docker network”中,但是当我这样做时:
docker-compose up
我有一个错误:
ERROR: Service "site_b" uses an undefined network "proxy-nginx"
编辑2:
以下是我在开始时使用nginx-proxy容器的日志:
forego | starting dockergen.1 on port 5000
forego | starting nginx.1 on port 5100
dockergen.1 | 2017/10/25 14:01:53 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
dockergen.1 | 2017/10/25 14:01:53 Running 'nginx -s reload'
nginx.1 | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container1" has suspicious symbols in /etc/nginx/conf.d/default.conf:60
nginx.1 | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container2" has suspicious symbols in /etc/nginx/conf.d/default.conf:74
dockergen.1 | 2017/10/25 14:01:54 Watching docker events
dockergen.1 | 2017/10/25 14:01:54 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
编辑3:
所以,现在,我的nginx配置看起来像:
server {
listen 192.168.12.28;
server_name 192.168.12.28;
access_log /var/log/nginx/host.access.log;
location / {
proxy_http_version 1.1;
proxy_pass http://172.23.0.3:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
location /container1/ {
proxy_http_version 1.1;
proxy_pass http://172.23.0.3:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
location /container2/ {
proxy_http_version 1.1;
proxy_pass http://172.23.0.2:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
}
当我去http://192.168.12.28/container1
时我的日志是:
site_a_1 | 172.23.0.1 - - [26/Oct/2017:09:22:40 +0000] "GET /container1/ HTTP/1.1" 404 449 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
当我去http://192.168.12.28/container2
时我的日志是:
site_b_1 | 172.23.0.1 - - [26/Oct/2017:09:22:50 +0000] "GET /container2/ HTTP/1.1" 404 449 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
对于两者,响应是404未找到。它似乎在我的容器的服务器中查看“/ container1”或“/ container2”路径?如果这是真的,我可以强迫他照顾/ var / www / html /?
答案 0 :(得分:1)
我不知道jwilder的代理可以自定义多远,但我知道VIRTUAL_HOST
环境变量是指您尝试连接的整个DNS名称。
目前,您的nginx已配置为将流向http://container1和http://container2的流量重新路由到容器中。但由于这些DNS别名不太可能被设置,因此会导致503错误。
您需要使用服务器块和两个/三个位置块自己配置nginx。以下配置将您的服务器端口80代理到容器1,将http://your_host/container1代理到容器1,将http://your_host/container2代理到容器2(我希望)。您必须将server_name
更改为您的主机名称:
server {
listen 80;
server_name your_fqdn.domain.com your_shortname;
access_log /var/log/nginx/host.access.log main;
location / {
proxy_http_version 1.1;
proxy_pass http://container1:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
location /container1/ {
proxy_http_version 1.1;
proxy_pass http://container1:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
location /container2/ {
proxy_http_version 1.1;
proxy_pass http://container2:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
}
关于撰写文件中的编辑和泊坞网络:
您不需要指定网络,因为docker-compose会自动为您的所有容器创建一个单独的网络。如果您想使用现有网络,或者您希望将容器放在不同的网络中,我建议您阅读this