天使漫步IT工作室天使漫步IT工作室

【ci】CodeIgniter 出现ERR_CONTENT_DECODING_FAILED解决办法


Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/u11u.com/usr/themes/wq/functions.php on line 110

Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/u11u.com/usr/themes/wq/functions.php on line 116

一、bug描述

今天在git上下载了一个CodeIgniter为框架的demo,运行后出现如图的bug。

WX20171225-143734@2x.png

内容为:

无法访问此网站
网址为 http://shiwen.localhost.com/index.php/gushi/show 的网页可能暂时无法连接,或者它已永久性地移动到了新网址。
ERR_CONTENT_DECODING_FAILED

一开始怀疑是ci的路由问题,没有往ERR_CONTENT_DECODING_FAILED上想,因为apache的access.log日志显示都是200,即能够请求成功,能够定位得出是ci那边没有处理好导致的问题。

经过高人指点,定位问题点在于ERR_CONTENT_DECODING_FAILED这个关键词。google下发现是因为ci吐出来的内容经过压缩或者混淆(compress and mix content)操作,内容返回到浏览器导致解压失败

二、解决办法

  • 在ci system中搜索zip

我用的是phpstorm ide,利用ctrl+H全局搜索zip关键词,发现有关zip压缩配置的地方有几处,不过application(这是二次开发的地方)只有一处:

/*
|--------------------------------------------------------------------------
| Output Compression
|--------------------------------------------------------------------------
|
| Enables Gzip output compression for faster page loads.  When enabled,
| the output class will test whether your server supports Gzip.
| Even if it does, however, not all browsers support compression
| so enable only if you are reasonably sure your visitors can handle it.
|
| Only used if zlib.output_compression is turned off in your php.ini.
| Please do not use it together with httpd-level output compression.
|
| VERY IMPORTANT:  If you are getting a blank page when compression is enabled it
| means you are prematurely outputting something to your browser. It could
| even be a line of whitespace at the end of one of your scripts.  For
| compression to work, nothing can be sent before the output buffer is called
| by the output class.  Do not 'echo' any values with compression enabled.
|
*/
$config['compress_output'] = TRUE;

英文的意思是,这个compress_output字段为控制页面是否被Gzip以支持快速加载。抱着试试不会怀孕的想法把这个标志位改为FALSE,然后再从web上请求。最后ok!

  • 万能的stackoverflow.com

通过google找到了另一个解决办法,里面的题主不想关闭上一个标志位,因为压缩可以带来优越的网络传输性能。原文链接如下:

https://stackoverflow.com/questions/10971191/codeigniter-content-encoding-error

摘取答案内容如下:

This issue is where the output buffering starts. The check for the config variable is in system/core/Output.php in _display(). It starts the gzipped buffering after a lot of code has already run. This leaves the potential for output to have occurred before it buffering starts.

With compress_output set to false it doesn't matter because nothing is encoded. With it set to true you end up with mixed content. Some output is encoded and some is not which causes the compression error.

There are two solutions:

1) You could leave compress_output set to false and add ob_start('ob_gzhandler'); to the top of your index.php file. This will ensure that all output is always gzipped, including errors.

2) The other solution is to add ob_flush(); before ob_start('ob_gzhandler'); in system/Output.php. This will gzip output when there are no errors and serve you unencoded content when there are errors.

I think 2 is the better solution and should be implemented by the CodeIgniter team. But if you don't want to muck with the system code (changes will go away when you upgrade) then 1 is the better solution for you.

上面提到了为何产生这个问题的原因,请自行阅读或者翻译。

还提到答主比较认同第二种解决办法,即在ob_start('ob_gzhandler');之前加入ob_flush();,上面那个标志位可以根据业务和环境需求进行定制。

在system/core/Output.php文件中找到ob_start('ob_gzhandler');,在它之前插入代码,改完后如下:

// Is compression requested?
        if (isset($CI) // This means that we're not serving a cache file, if we were, it would already be compressed
            && $this->_compress_output === TRUE
            && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
        {
            ob_flush();//add a new line,fix zip mix content bug
            ob_start('ob_gzhandler');
        }

至此完美解决ci请求后出现ERR_CONTENT_DECODING_FAILED的问题。

本站原创,欢迎转载,转载敬请标明出处:天使漫步IT工作室 » 【ci】CodeIgniter 出现ERR_CONTENT_DECODING_FAILED解决办法
添加新评论


Warning: Use of undefined constant php - assumed 'php' (this will throw an Error in a future version of PHP) in /www/wwwroot/u11u.com/usr/themes/wq/comments.php on line 38