使用 nginx-proxy 容器作为前端服务器访问同一主机上的多个容器时出现 503 错误

使用 nginx-proxy 容器作为前端服务器访问同一主机上的多个容器时出现 503 错误

我希望能够访问同一主机上包含 Web 应用程序的多个容器。

当我想访问主机 (通过 IP 地址) 或容器 (例如通过 host_ip_adress/container1) 时,我都会收到 503 错误nginx。我想要的是通过 ip_addrress_host/container1 访问我的容器 1。

我在网上找到的解决方案是设置一个nginx-proxy前端服务器(来源:https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker/

我的docker-compose文件: docker-compose.yml

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=container1
  volumes:
   - ./php:/var/www/html
 site_b:
   image: php:7.0-apache
   expose:
   - "80"
   environment:
   - VIRTUAL_HOST=container2
   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 容器的日志:

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'

编辑2:我尝试使用 Paweł Tatarczuk 提供的配置文件来“定制” nginx-proxy(https://serverfault.com/a/880384/441157

现在,当我发出类似这样的请求时http://192.168.12.28/container1我得到了这个日志:

nginx-proxy_1  | nginx.1    | 2017/10/26 08:46:19 [error] 41#41: *1 open() "/etc/nginx/html/container1" failed (2: No such file or directory), client: 192.168.12.82, server: 192.168.12.28, request: "GET /container1 HTTP/1.1", host: "192.168.12.28"

编辑3:将 ? 添加到重写中

nginx-proxy_1  | nginx.1    | 2017/10/26 09:11:00 [error] 31#31: *1 container1 could not be resolved (2: Server failure), client: 192.168.12.82, server: 192.168.12.28, request: "GET /container1 HTTP/1.1", host: "192.168.12.28"

答案1

我想要的是通过 ip_addrress_host/container1 访问我的 container1。

您确定要使用例如访问容器吗http://127.0.0.1/container1? 那么就jwilder/nginx-proxy不是最好的办法了。

您的代理正在本地监听端口 80,它将代理请求到container1和,container2但不会代理路径/container1/container2

卷曲

curl -H "Host: container1" localhost

浏览器

打开http://container1


您可以附加一个自定义配置,该配置将负责处理代理路径以使ip_address/container-name工作正常进行:

  1. 将其添加到volumes./custom.conf:/etc/nginx/conf.d/custom.conf
  2. custom.conf在旁边创建docker-compose.yml

    server {
        server_name 192.168.X.Y;
        listen 80;
    
        location ~ ^/([^/]+)(/.*)? {
            proxy_pass http://$1$2;
        }
    }
    
  3. 链接容器

    nginx-proxy:
      ...
      links:
        - site_a:container1
        - site_b:container2
    

这只是开始,你应该改进它以满足你的需求。它应该适用于http://192.168.XY/container1或者http://192.168.XY/container2

请注意,有一个重写,因此http://192.168.XY/container1/some/path代理至 http://container1/some/path。我假设您不想要带有/container1前缀的请求到您的目标容器。

相关内容