docker-compose:varnish apache2返回503错误`backend fitch failed'



我正在尝试基于varnishphp7.1+apache2服务运行一个非常简单的Docker-compose.yml文件:

version: "3"
services:
  cache:
    image: varnish
    container_name: varnish
    volumes:
      - ./default.vcl:/etc/varnish/default.vcl
    links:
      - web:webserver
    depends_on:
      - web
    ports:
      - 80:80
  web:
    image: benit/stretch-php-7.1
    container_name: web
    ports:
      - 8080:80
    volumes:
      - ./index.php:/var/www/html/index.php

default.vcl包含:

vcl 4.0;
backend default {
  .host = "webserver";
  .port = "8080";
}

我在http://localhost/浏览时遇到以下错误:

Error 503 Backend fetch failed
Backend fetch failed
Guru Meditation:
XID: 9
Varnish cache server

我在http://localhost:8080/进行测试时,web服务正常。

怎么了?

您需要配置varnish以与端口" 80"中的" Web"进行通信,而不是端口" 8080"。

" Web"来自撰写文件中的服务名称。无需设置容器名称,实际上,如果您过渡到Swarm Mode,则打破了扩展或执行滚动更新的能力。链接已被弃用,以支持Docker Compose提供的共享网络(链接非常脆弱,如果您更新Web容器,则断开(。depends_on不保证其他服务准备接收请求。如果您从启动到Web服务器准备接收请求很难持有清漆Web服务器下降。

端口80来自容器端口。如果您只想通过Varnish访问Docker主机,则无需在Docker主机上发布端口8080,这对许多人来说就是安全风险。容器直接通信到容器端口,而不是回到主机并映射回容器中。

由此产生的组合文件看起来像:

version: "3"
services:
  cache:
    image: varnish
    container_name: varnish
    volumes:
      - ./default.vcl:/etc/varnish/default.vcl
    ports:
      - 80:80
  web:
    image: benit/stretch-php-7.1
    volumes:
      - ./index.php:/var/www/html/index.php

,重要的是,您的清漆配置看起来像:

vcl 4.0;
backend default {
  .host = "web";
  .port = "80";
}

最新更新