强制高速caching处理器输出积极抵制caching

我试图强制caching一个非常令人讨厌的PHP脚本,通过积极地设置所有的反高速caching头,积极地尝试抵制caching,没有任何理由。

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Content-Type: text/html; charset=UTF-8 Date: Thu, 22 May 2014 08:43:53 GMT Expires: Thu, 19 Nov 1981 08:52:00 GMT Last-Modified: Pragma: no-cache Set-Cookie: ECSESSID=...; path=/ Vary: User-Agent,Accept-Encoding Server: Apache/2.4.6 (Ubuntu) X-Powered-By: PHP/5.5.3-1ubuntu2.3 

如果完全可以避免的话,我不想修改这个第三方的代码片段,而只是让Apachecaching页面一段时间。 我非常有select性地只对那些对会话cookie等没有实际影响的非常特定的页面这样做,即不包含任何个性化信息。

 CacheDefaultExpire 600 CacheMinExpire 600 CacheMaxExpire 1800 CacheHeader On CacheDetailHeader On CacheIgnoreHeaders Set-Cookie CacheIgnoreCacheControl On CacheIgnoreNoLastMod On CacheStoreExpired On CacheStoreNoStore On CacheLock On CacheEnable disk /the/script.php 

Apache caching页面好吧:

 [cache:debug] AH00698: cache: Key for entity /the/script.php?(null) is http://example.com:80/the/script.php? [cache_disk:debug] AH00709: Recalled cached URL info header http://example.com:80/the/script.php? [cache_disk:debug] AH00720: Recalled headers for URL http://example.com:80/the/script.php? [cache:debug] AH00695: Cached response for /the/script.php isn't fresh. Adding conditional request headers. [cache:debug] AH00750: Adding CACHE_SAVE filter for /the/script.php [cache:debug] AH00751: Adding CACHE_REMOVE_URL filter for /the/script.php [cache:debug] AH00769: cache: Caching url: /the/script.php [cache:debug] AH00770: cache: Removing CACHE_REMOVE_URL filter. [cache_disk:debug] AH00737: commit_entity: Headers and body for URL http://example.com:80/the/script.php? cached. 

但是,它始终坚持“caching响应不新鲜”,并且从不提供caching版本。 我这与Expires头文件有关,它将文档标记为已过期(但我不知道这是否是正确的假设)。 我试图覆盖和使用mod_headers取消设置标题,但这并没有帮助; 我尝试caching的任何组合都没有留下深刻的印象。 我猜测操作的顺序是错误的,并且caching看到它们之后 ,标题正在被重写。 early头文件处理也无济于事。 我已经尝试了CacheQuickHandler Off并尝试设置显式filter链,但没有任何帮助。 但是我实际上大部分都是在黑暗中瞎猜,因为我没有很多configurationApachefilter链的经验。

有一个简单的解决scheme,如何caching这个令人讨厌的代码片段?

空的Last-Modified:标头使请求不可caching。 所以首先要做的是生成一个,一个解决scheme是使用php auto_prepend_file

创build一个像这样的内容的文件prepend.php

   gmdate(“D,d MYH:i:s”),“GMT”);  ?>

然后添加你的虚拟主机configuration的指令: php_value auto_prepend_file path_to_prepend.php

此时,您必须validation服务器响应是否有正确的Last-Modified:标头。 如果没有,我们将无法caching它,如果是的话,也许你的工作mod_headers和mod_cache现在工作?

如果没有,你可以像这样使用squid和apache:

Apacheconfiguration

在正确的虚拟主机中,只需启用mod_rewrite并使用它来redirect您要caching的stream量:

 <VirtualHost your_current_virtual_host:80>
  ServerName your.site.com
  ..
 重写引擎

  #这使得caching服务器可以将请求视为http://your.site.com/。
  ProxyPreserveHost

  #这应该在虚拟主机级别,而不是&lt; Directory或.htaccess


  #DoSquid envvariables决定我们是否将请求发送到caching服务器
  #根据您的需要调整它
  RewriteRule /the/script.php  -  [E = DoSquid:是]

  #POSTs不可caching
  RewriteCond%{REQUEST_METHOD} ^ POST $ 
  RewriteRule。*  -  [E:DoSquid:否]

  #随意添加任何适合您的需求的规则

  #来自本地主机的请求是来自“主”虚拟主机的呼叫(见下文)
  RewriteCond%{REMOTE_ADDR} ^ 127 \ .0 \ .0 \ .1 $ [E:DoSquid:否]

  RewriteCond%{ENV:DoSquid} ^是$
  RewriteRule /the/script.php http://ip_of_caching_server/this/script.php [P,L,QSA]

  ..
  ..
 <虚拟主机/>

 #这个VirtualHost将被你的caching服务器作为你的站点的主服务器访问
 #端口8009可以是任何东西,它只是一个单独的虚拟主机

 <VirtualHost your_current_virtual_host:8009>
  ServerName your.site.com
  ..
 重写引擎

  #这里有一个mod_headers的大量使用,以便有一个可caching的响应
 不用说,这可能会彻底破坏你的应用程序。 答复是
  #完全匿名

 标题未设置Set-Cookie
 标题未设置Etag
 标题未设置Pragma
  RequestHeader未设置Cookie

  #现在修复Cache-Control标头
 头合并Cache-Control public
  #最大年龄是一个痛苦。 如果它没有设置,我们必须设置一个,如果它是0,我们必须改变它
 标题合并caching控制“max-age = bidon”
  #如果我们有:Cache-Control max-age = ..,....
 标题编辑Cache-Control“^(。*)max-age =(。*)max-age = bidon,(。*)$”$ 1max-age = $ 2 $ 3
  #如果我们有:Cache-Control yyy = bidon,max-age = ..“
 标题编辑Cache-Control“^(。*)max-age =(。*),max-age = bidon $”$ 1max-age = $ 2
  #现在如果没有最大年龄,设置为10mn,则replace该值
 标题编辑caching控制“max-age = bidon”“max-age = 600”
  #现在,如果max-age = 0,设置为10mn,则更换该值
 标题编辑caching控制“max-age = 0”“max-age = 600”

  #删除caching控制参数,防止caching
 标题编辑caching控制“no-cache”,“”
 标题编辑Cache-Control“no-store”,“”
 标题编辑Cache-Control“post-check = 0”,“”
 标题编辑Cache-Control“pre-check = 0”,“”
 标题编辑caching控制“必须重新validation”,“”

  #请求现在被转发到第一个虚拟主机。 它不会循环,因为我们不caching来自127.0.0.1的请求
  ProxyPreserveHost
  RewriteRule ^(。*)$ http://127.0.0.1/$1 [P,L,QSA]

  ..
  ..
 <虚拟主机/>

caching服务器configuration

你可以使用任何东西:鱿鱼,阿帕奇,清漆。 与鱿鱼,你必须将其configuration为反向代理和声明cache_peer your.site.com parent 8009 0 no-query originserver ..也许你可以在第二个虚拟主机启用mod_cache来实现你想要的。