1

解决方案位于:https ://serverfault.com/questions/880212/error-503-when-using-nginx-proxy-container-like-a-front-end-server-to-access-mul

我希望能够访问同一主机上包含 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/ )

我的码头工人撰写文件:

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网络”中,但是当我愿意 :

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 not found。似乎它查看了我的容器服务器的“/container1”或“/container2”路径?如果是真的,我可以强迫他照顾 /var/www/html/ 吗?

4

1 回答 1

1

我不知道 jwilder 的代理可定制多远,但我知道VIRTUAL_HOST环境变量指的是您尝试连接的整个 DNS-Name。

当前,您的 nginx 配置为将流向http://container1http://container2的流量重新路由到容器中。但由于这些 DNS-Alias 不太可能设置,因此会导致 503 错误。

您需要自己配置您的 nginx,使用一个服务器块和两个/三个位置块。以下配置将您的服务器端口 80 代理到 container1,将http://your_host/container1代理到container1 ,将 http://your_host / container2 代理到 container2(我希望如此)。但是,您必须更改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 网络:

您不需要指定网络,因为 docker-compose 会自动为您的所有容器创建一个单独的网络。如果您想使用已经存在的网络,或者您想将您的容器放在不同的网络中,我建议您阅读这篇文章

于 2017-10-25T13:14:20.740 回答