本地开发中Gzip无法正常工作

3

我正在使用Kohana缩小器模块将less / css和js文件压缩成类型组合文件。

/less/f31e419e939bfec51b4fe8322f545455.less?r=20130618 /js/24dae189814109f20ff9a5bf4422be36.js?r=20130618

我正在使用MAMP PRO,但在Firefox和Chrome上收到以下错误:

Content Encoding Error
This webpage is not available

内容头部信息如下:
HTTP/1.1 200 OK
Date: Tue, 18 Jun 2013 11:17:14 GMT
Server: Apache
X-Powered-By: PHP/5.4.4
Etag: "acf11d952c07adb223fa77f3d385f56e"
Cache-Control: must-revalidate
Last-Modified: Thu, 13 Jun 2013 21:57:04 +0000
Content-Encoding: gzip
Content-Length: 16894
Set-Cookie: session_database=ecfc139402165669a4b6d5e8190564231133fb0d%7E51c041ba9f6e95-28008580; expires=Wed, 19-Jun-2013 11:17:17 GMT; path=/
Content-Type: text/css; charset=utf-8

如果我去掉gzip编码,内容就可以正常工作,但显然我想利用gzip压缩。我的PHP代码如下:

// Further up the page
$data['cache_gz'] = gzencode($data['cache']);

//...

if ($gzip === FALSE){
    $this->response->body($data['cache']); // Works
}else{
    this->response->headers('Content-Encoding', $gzip); // Doesn't Work

    $this->response->headers('Content-Length', strlen($data['cache_gz']));
    $this->response->body($data['cache_gz']);
}

希望你能给我提供一些指导,因为我无法解决这个问题,或者这个问题是否只与本地服务器有关。


你在本地的Apache上启用了mod_deflate吗? - herrhansen
是的,所有的模块都已经加载,包括mod_deflate。 - AltDan
1
在php.net的gzencode函数手册中,有一条评论建议在gzencode之前添加ini_set('zlib.output_compression','Off');。为什么即使gzip标志为False,你也要使用gzencode(...)呢? - 0xBAADF00D
确保关闭所有错误消息,并消除文件结尾和开头的空格,还要确保 mod_deflate 不会对已经编码的数据进行重新编码。 - tlogbon
2个回答

0

这可能只是您发布的一个笔误,但是

this->response->headers('Content-Encoding', $gzip);

应该是

$this->response->headers('Content-Encoding', $gzip);

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接