我如何使用 mod_rewite 来使用页面的缓存副本(如果可用),否则运行动态脚本?

我如何使用 mod_rewite 来使用页面的缓存副本(如果可用),否则运行动态脚本?

我想将 php 脚本的生成输出保存到 apache 首先检查的文件夹结构中。

/about/index.php <- dynamic should be rendered only if cache below does not exist
/contact/index.php

/public/about/index.html  <- cached should be rendered first if exists
/public/contact/index.html

我如何创建一个 RedirectCond/RedirectRule 集来为我执行此操作?

答案1

让网站指向缓存目录。设置一条规则,如果文件不存在,则重写到“动态”源目录。动态脚本还必须负责写入缓存目录(假设您希望将其缓存)。其他程序必须负责从缓存中删除过时的条目(cron 作业等)。

更好的想法是在 Apache 前面放置一个实际的缓存反向代理。可以使用 Apache 作为反向代理,但还有更好、更合适的工具,如 nginx 或 varnish。

相关内容