在 Apache 配置中有条件地设置过期标头

发布于 2024-11-16 06:43:52 字数 473 浏览 4 评论 0原文

我想有条件地在图像上设置过期标头,以便它们在项目开发过程中不会缓存,但在生产过程中会缓存。理想情况下,这只是对 apache conf 文件的修改。我有一个 perl 脚本,它将返回项目的状态,它可以与 mod_rewrite 一起使用,如下所示:

rewritemap  PSTAT prg:/bin/pstat.pl
...skipping...
rewritecond ${PSTAT:$site:$1} =devel
rewriterule ^/run/$site/p(\d+)/(\w+) /logout.pl/$2 [NS,L]

如果我可以做类似的事情,那就太好了:

rewritecond ${PSTAT:$site:$1} =devel
ExpiresByType image/jpg "now plus 1 second"

虽然这当然行不通。

有什么解决办法吗?

I would like to conditionally set expires headers on images so that they will not cache while a project is in development but will when it is in production. Ideally this would just be a modification of the apache conf file. I have a perl script that will return the status of the project, which can be used with mod_rewrite as follows:

rewritemap  PSTAT prg:/bin/pstat.pl
...skipping...
rewritecond ${PSTAT:$site:$1} =devel
rewriterule ^/run/$site/p(\d+)/(\w+) /logout.pl/$2 [NS,L]

It would be nice if I could do something like:

rewritecond ${PSTAT:$site:$1} =devel
ExpiresByType image/jpg "now plus 1 second"

Though of course that wouldn't work.

Is there any solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月寒剑心 2024-11-23 06:43:52

对我有用的一个技巧是首先无条件设置标头:

ExpiresByType image/jpg "now plus 1 second"
...

然后在我们处于开发模式时取消设置标头:

Header set Cache-control "no-cache" env=devel
Header unset expires env=devel

这要求您有一个预先根据您的模式初始化的布尔环境 devel 。在我们的例子中,我们决定是否要开发(devel.domain.com 与 www.domain.com)的主机名。

A trick that worked for me is to first set the headers unconditionally:

ExpiresByType image/jpg "now plus 1 second"
...

And then to unset the header in case we are in devel mode:

Header set Cache-control "no-cache" env=devel
Header unset expires env=devel

This requires that you have a boolean env devel previously initialized based on your mode. In our case we decide on the host name whether we want to be devel or not (devel.domain.com vs. www.domain.com).

沫离伤花 2024-11-23 06:43:52

从alienhard所说的开始,我设法找到了我的问题的答案。

rewritemap  PSTAT prg:/bin/pstat.pl
...skipping...
rewritecond ${PSTAT:$site:$1} =devel
rewriterule ^/images/(\d+)/(\w+) - [E=devel:1]

header set cache-control "no-cache" env=devel
header unset expires env=devel

/images/(\d+) 是特定项目编号 (\d+) 的图像文件夹)

E 标志>rewriterule 允许您在规则匹配的情况下设置环境变量。 - 实际上并没有重写任何东西。因此,这会使用 rewritecond 向其发送来自 rewriterule 的项目编号来检查脚本的输出,然后在两个条件都匹配的情况下设置环境变量。然后根据该环境变量的存在有条件地设置 header

Starting from what alienhard said I managed to come up with an answer to my problem.

rewritemap  PSTAT prg:/bin/pstat.pl
...skipping...
rewritecond ${PSTAT:$site:$1} =devel
rewriterule ^/images/(\d+)/(\w+) - [E=devel:1]

header set cache-control "no-cache" env=devel
header unset expires env=devel

(/images/(\d+) is the folder of images for a particular project number (\d+))

The E flag of rewriterule lets you set an environment variable in the case that the rule matches. - doesn't actually rewrite anything. Thus, this checks the output of the script using rewritecond sending it the project number from the rewriterule, and then sets the environment variable in the case that both conditions match. Then header conditionally gets set based on the presence of that environment variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文