繁体   English   中英

Docker / Varnish / Hitch / Nginx / PHP:清除特定页面缓存的正确方法是什么?

[英]Docker / Varnish / Hitch / Nginx / PHP : What is correct way to purge cache for specific page?

我通过 docker 使用hitch、varnish、nginx、phpmysql容器设置了简单的 php 应用程序。 为了使Hitch在本地工作,我通过mkcert实用程序为添加到/etc/hostsmy.varnish.test本地域生成了自签名证书

所以https://my.varnish.test在本地工作正常。

这是应用程序的 github: https://github.com/aposidelov/hitch-varnish-local-example

该应用程序执行以下操作:

  • 列表页面(/index.php),其中显示了 mysql 表中的所有行。
  • 添加允许向 mysql 表添加新行的表单(/user_add_form.php)。

我希望通过清漆缓存列表页面,并且在提交添加表单后我希望清除列表页面 目前/index.php由清漆缓存,但我在提交表单后如何清除特定页面时遇到问题。

清除缓存的正确方法是什么?

我找到了通过使用 PURGE 方法调用 curl 来实现的方法。

exec('curl -I -X PURGE https://my.varnish.test/index.php', $output, $retval);

但是当我在我的项目中使用 docker 时,这意味着https://my.varnish.test在 nginx 容器中不可用(至少在我的本地机器上)。 If I go inside my nginx container and try curl https://my.varnish.test/index.php it doesn't see it, instead it's can see only curl http://nginx_server/index.php but purge via exec('curl -I -X PURGE http://nginx_server/index.php', $output, $retval); 不起作用。

您的 Git 存储库有一个default.vcl ,其中包含执行清除所需的代码。

但是,不会达到清除逻辑,因为较早的 if 语句会导致提前退出。

考虑您的 Git 存储库中的这段代码:

sub vcl_recv {
  if (req.method != "GET" && req.method != "HEAD") {
    return (pass);
  }
  if (req.method == "PURGE") {
    if (!client.ip ~ purge) {
      return (synth(405, client.ip + " is not allowed to send PURGE requests."));
    }
    return (purge);
  }
}

PURGE请求方法将匹配第一个 if 语句(不是GET也不是HEAD )并将导致return(pass); 被调用。

请将此 if 语句放在您的清除逻辑之后,您将能够执行清除。

另一个障碍可能是限制为本地 IP 地址的 ACL,如下例所示:

acl purge {
  "localhost";
  "127.0.0.1";
  "::1";
}

我的建议是查看 Docker 设置的拓扑结构,并可能添加将执行PURGE请求的系统的 IP 地址。 这可能是由 Docker 发出的 IP 地址。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM