【问题标题】:PHP Dynamic Files Caching ProblemPHP动态文件缓存问题
【发布时间】:2011-09-16 21:30:12
【问题描述】:

我的 .htaccess 中有一些由 PHP 动态生成的 CSS 和 JS 文件:

AddHandler application/x-httpd-php .css .js

然后在文件里面,例如:

<?PHP if ($Browser == 'msie') { ?>
.bind('selectstart', function(event) { [...] })
<?PHP } ?>

在它们的顶部(第一行),我使用基于 Last-Modified 标头的条件获取,如下所示:

<?PHP if (FileIsCached(getlastmod())) die(); ?>

这是函数:

public static function FileIsCached($Timestamp)
{
    $LastModified = substr(date('r', $Timestamp), 0, -5).'GMT';
    $IfModifiedSince = (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);

    if (($Semicolon = strrpos($IfModifiedSince, ';')) !== false)
        $IfModifiedSince = substr($IfModifiedSince, 0, $Semicolon);

    header('Last-Modified: '.$LastModified);

    if (!$IfModifiedSince || ($IfModifiedSince != $LastModified))
        return false;

    header('HTTP/1.1 304 Not Modified');

    return true;
}

我不需要检查编码,因为我在 php.ini 中使用了自动 gzip:

 output_handler = ob_gzhandler

我通常将它们放在我的 index.php 中,如下所示:

<script type="<?PHP echo $JavascriptMIME; ?>" src="/script.js"></script>

一切都像一个魅力......这是我的第一个加载响应头:

HTTP/1.1 200 OK
Date: Fri, 16 Sep 2011 21:21:51 GMT
Last-Modified: Fri, 16 Sep 2011 19:25:37 GMT
Content-Encoding: gzip
Vary: Accept-Encoding
Cache-Control: max-age=31536000, public
Expires: Sat, 15 Sep 2012 21:21:51 GMT
Content-Type: application/javascript
Content-Length: 6161
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive

这是我缓存的响应头:

HTTP/1.1 304 Not Modified
Date: Fri, 16 Sep 2011 21:24:39 GMT
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Expires: Sat, 15 Sep 2012 21:24:39 GMT
Cache-Control: max-age=31536000, public
Vary: Accept-Encoding

问题在于,有时 HTTP 数据似乎被弄乱了,可能是 gzip 压缩的内容或类似的东西。 Chrome 网络控制台有时会显示以下错误:“资源解释为其他,但传输的 MIME 类型未定义”。 Mozilla 有时会向 index.php 发出 2 个请求,或者尝试在下载提示下下载它。有时,当我通常加载 7 个文件时,文件请求会在 5 处停止,而最后下载的文件内容会像这样:

���������ÿÿ���������HTTP/1.1 200 OK
Date: Fri, 16 Sep 2011 19:53:26 GMT
Server: Apache/2.2.17 (Win32) PHP/5.3.6
X-Powered-By: PHP/5.3.6
Cache-Control: must-revalidate, no-cache, no-store, private, public
Cache-Control: max-age=0, max-stale=0, post-check=0, pre-check=0
Expires: Wed, 11 Apr 1984 18:36:00 GMT
Expires: 0
Last-Modified: Fri, 16 Sep 2011 19:53:26 GMT
Pragma: no-cache
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Type: application/javascript
Content-Length: 674
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
....

会是什么?

【问题讨论】:

    标签: php apache .htaccess caching http-headers


    【解决方案1】:

    尝试在 PHP 中关闭输出处理程序,看看是否一切正常。

    我在 php 手册页上找到了 this comment,这可能就是您遇到的问题。

    您可以尝试一下,如果是问题,请使用 Apache 的 SetOutputFilter 通过将以下内容添加到 httpd.conf 来为您压缩内容,而不是让 PHP 这样做

    <Location />
    # Insert filter
    SetOutputFilter DEFLATE
    
    # Netscape 4.x has some problems...
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    
    # Netscape 4.06-4.08 have some more problems
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    
    # MSIE masquerades as Netscape, but it is fine
    # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    
    # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
    # the above regex won't work. You can use the following
    # workaround to get the desired effect:
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
    
    # Don't compress images
    SetEnvIfNoCase Request_URI \
    \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
    </Location>
    

    【讨论】:

    • 您是否在此处建议了处理 Netscape 4 的代码? Apache 2.0.48 于 2003 年问世!
    猜你喜欢
    • 2010-12-18
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2011-11-22
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多