简体   繁体   English

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

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

I have setup simple php app via docker by using hitch, varnish, nginx, php and mysql containers.我通过 docker 使用hitch、varnish、nginx、phpmysql容器设置了简单的 php 应用程序。 To make Hitch work locally I've generated self-signed certificate via mkcert utility for my.varnish.test local domain that was added to /etc/hosts为了使Hitch在本地工作,我通过mkcert实用程序为添加到/etc/hostsmy.varnish.test本地域生成了自签名证书

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

Here is github of the app: https://github.com/aposidelov/hitch-varnish-local-example这是应用程序的 github: https://github.com/aposidelov/hitch-varnish-local-example

The app does following:该应用程序执行以下操作:

  • Listing page (/index.php) where all rows from mysql table are shown.列表页面(/index.php),其中显示了 mysql 表中的所有行。
  • Add form (/user_add_form.php) that allows to add new row to mysql table.添加允许向 mysql 表添加新行的表单(/user_add_form.php)。

I want Listing page to be cached via varnish and after Add form is submitted I want Listing page to be purged.我希望通过清漆缓存列表页面,并且在提交添加表单后我希望清除列表页面 For now /index.php is cached by varnish but I have problems how to purge specific page after form is submitted.目前/index.php由清漆缓存,但我在提交表单后如何清除特定页面时遇到问题。

What is correct way to purge a cache?清除缓存的正确方法是什么?

I've found approach by doing it using curl call with PURGE method.我找到了通过使用 PURGE 方法调用 curl 来实现的方法。

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

But as I use docker in my project it means that https://my.varnish.test is not available from nginx container (at least on my local machine).但是当我在我的项目中使用 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); 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); doesn't work.不起作用。

Your Git repo has a default.vcl that has the necessary code in it to perform the purge.您的 Git 存储库有一个default.vcl ,其中包含执行清除所需的代码。

However, the purge logic will not be reached because an earlier if-statement will result in an early exit.但是,不会达到清除逻辑,因为较早的 if 语句会导致提前退出。

Consider this code that is in your Git repo:考虑您的 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);
  }
}

The PURGE request method will match the first if-statement (not a GET and not a HEAD ) and will result in return(pass); PURGE请求方法将匹配第一个 if 语句(不是GET也不是HEAD )并将导致return(pass); being called.被调用。

Please put this if-statement after your purge logic and you'll be able to perform purges.请将此 if 语句放在您的清除逻辑之后,您将能够执行清除。

Another hurdle might be the ACL that is restricted to local IP addresses as you can see in the example below:另一个障碍可能是限制为本地 IP 地址的 ACL,如下例所示:

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

My advice is to look at the topology of your Docker setup and potentially add the IP address of the system that will execute the PURGE request.我的建议是查看 Docker 设置的拓扑结构,并可能添加将执行PURGE请求的系统的 IP 地址。 This could be an IP addressed issued by Docker.这可能是由 Docker 发出的 IP 地址。

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

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