简体   繁体   English

如何使mod_cache与动态内容一起正常工作?

[英]How to make mod_cache work properly with dynamic content?

I'm trying to use mod_cache to cache dynamically generated content. 我正在尝试使用mod_cache来缓存动态生成的内容。 This is my Apache config: 这是我的Apache配置:

CacheEnable mem /
MCacheSize 4096
MCacheMaxObjectCount 100
MCacheMinObjectSize 1
MCacheMaxObjectSize 2048
CacheIgnoreCacheControl On
CacheIgnoreNoLastMod On
CacheStorePrivate On
CacheStoreNoStore On

<Location /cgi-bin>
    SetHandler cgi-script
    Options +ExecCGI
</Location>

And this is one CGI script (just for testing): 这是一个CGI脚本(仅用于测试):

#!/opt/app/phantomjs/bin/phantomjs
var date = new Date('Sun, 01 Jan 2012 00:00:00 GMT');
console.log('Last-Modified: '+ date.toUTCString());
console.log('Cache-Control: max-age=' + (365 * 24 * 60 * 60)+ ', public');
date.setDate(date.getDate() + 365);
console.log('Expires: '+ date.toUTCString() + '\n\n');

// lengthy operation here...

console.log(content);

This basically works. 这基本上有效。 But what happens is if a client requests a cgi-bin with an If-Modified-Since header: 但是如果客户端请求带有If-Modified-Since标头的cgi-bin, 会发生什么

  1. CGI-script streams headers CGI脚本流标头
  2. some seconds delay 几秒钟延迟
  3. CGI-script streams body CGI脚本流体
  4. Apache sends 304 Apache发送304

That makes no sense to me. 这对我来说毫无意义。 Apache waits for the whole response before it sends a not-modified response. Apache在发送未修改的响应之前等待整个响应。

What I expected : 我的期望

  1. CGI-script streams headers CGI脚本流标头
  2. Apache sends 304 Apache发送304
  3. CGI-script is canceled or body of CGI-script is discarded CGI脚本被取消或CGI脚本的主体被丢弃

Is there any way to get it done? 有没有办法完成它?

The CGI script should be changed as follows. 应如下更改CGI脚本。

  1. Don't use a static Last-modified date. 不要使用静态的上次修改日期。 The Last-Modified should be set for dynamic resources to the current execution time. 应将动态资源的Last-Modified设置为当前执行时间。

  2. Cache Control directive contains a type error 'pulic' should be 'public' Cache Control指令包含一个类型错误'pulic'应该是'public'

  3. Expires directive is not needed, because Cache-Control has a higher priority than Expires. 不需要Expires指令,因为Cache-Control的优先级高于Expires。

Your expected behavior "terminate CGI script" after sending header is not possible. 发送标头后,您的预期行为“终止CGI脚本”是不可能的。 If your cache works, the CGI script is not executed until the cache entry is invalidated. 如果缓存有效,则在缓存条目无效之前不会执行CGI脚本。

This is the solution i'm talking in my last comment. 这是我在上次评论中所说的解决方案。 Forward the CGI execution to an internal virtual host. 将CGI执行转发到内部虚拟主机。 With this configuration mod_cache will work as expected. 使用此配置,mod_cache将按预期工作。 I tested it on my machine with apache 2.2.21. 我用apache 2.2.21在我的机器上测试了它。

# virtual cgi host - used internally only for cgi execution
<VirtualHost *:8080>
    ##ServerAdmin postmaster@dummy-host.localhost
    DocumentRoot "C:/Project/web"
    ServerName cgi-bin.local
    ErrorLog "logs/cgi-bin-error.log"
    CustomLog "logs/cgi-bin-access.log" combined
    LogLevel debug

    <Location /cgi-bin>
        SetHandler cgi-script
        Options +ExecCGI
    </Location>

</VirtualHost>

# Virtual host used by client
<VirtualHost *:8080>
    ##ServerAdmin postmaster@dummy-host.localhost
    DocumentRoot "C:/Project/web"
    ServerName web.local
    ErrorLog "logs/web-error.log"
    CustomLog "logs/web-access.log" combined

    CacheEnable mem /
    MCacheSize 4096
    MCacheMaxObjectCount 100
    MCacheMinObjectSize 50
    MCacheMaxObjectSize 20480
    MCacheMaxStreamingBuffer 20480
    CacheIgnoreCacheControl On
    CacheIgnoreNoLastMod On
    CacheStorePrivate On
    CacheStoreNoStore On

    ProxyRequests Off
    ProxyPass /cgn-bin http://cgi-bin.local:8080/
    ProxyPassReverse /cgn-bin http://cgi-bin.local:8080/

</VirtualHost>

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

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