如何让Apache的mod_deflate模块和传输编码:分块一起工作? [英] How to make Apache mod_deflate and Transfer-encoding : Chunked work together?

查看:288
本文介绍了如何让Apache的mod_deflate模块和传输编码:分块一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用bigpipe概念在我们的网站。这意味着要发送以块的响应,而不是将其发送作为一个整体,以使用户感觉该网页是快的。我成功地这样做,通过使用Java中的响应对象上flushBuffer方法。但现在,当我尝试COM preSS与Apache模块mod_deflate模块的内容,分块丢失。

I am trying to use the bigpipe concept on our website. That means trying to send the response in chunks instead of sending it as a whole so that user feels that page is fast. I am successful in doing that by using the flushBuffer method on the response object in java. But now when I try to compress the content with apache mod_deflate module, chunking is lost.

下面是Apache用于COM preSS内容配置

Here is the configuration from apache used to compress the content

**

DeflateBufferSize 100
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog /var/log/httpd/deflate_log deflate

结束mod_deflate模块配置**

下面是当放气在阿帕奇打开响应头

End mod_deflate config**

Here is the response header when the deflate is turned on in apache

连接:保持活动结果
内容编码:gzip 结果
内容长度:7916结果
内容类型:text / html的;字符集= UTF-8结果
日期:星期五,2012 1月27日20点11分十一秒GMT结果
保持活动:超时= 300,最大= 3997结果
服务器:Apache结果
有所不同:接受编码

Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:7916
Content-Type:text/html; charset=UTF-8
Date:Fri, 27 Jan 2012 20:11:11 GMT
Keep-Alive:timeout=300, max=3997
Server:Apache
Vary:Accept-Encoding

当放气已在Apache关闭响应头

Response header when the deflate is turned off in apache

连接:保持活动结果
内容类型:text / html的;字符集= UTF-8结果
日期:星期五,2012 1月27日20时21分14秒GMT结果
保持活动:超时= 300,最大= 3997结果
服务器:Apache / 2.2.3(CentOS的)结果
传输编码:分块

正如你可以看到上面2头分块工作只有COM pression被关闭。我在寻找互联网上并对此人建议,以减少在 DeflateBufferSize 值。我的价值下降到100字节,你可以在我的Apache配置看,但是仍然没有解决问题。 DeflateBufferSize设置为100个字节表示的反应是在Apache中的缓冲,直到被接收和100字节则是COM pressed。

As you can see in above 2 headers chunking is working only if the compression is turned off. I was searching on internet regarding this and people were suggesting to decrease the DeflateBufferSize value. I decreased the value to 100 bytes as you can see in my apache config but that still didn't solve the problem. DeflateBufferSize set to 100 bytes means that response is buffered in apache till 100 bytes are received and then the it is compressed.

我一直在寻找这是与旧的Apache 1.3捆绑mod_gzip的模块和模块具有以下指令它允许分块内容进行gzip压缩。

I was looking at the mod_gzip module which was bundled with the old apache 1.3 and that module has a following directive which allows chunked content to be gzipped.

mod_gzip_dechunk是

mod_gzip_dechunk Yes

有谁知道在这样的mod_deflate模块与指令Apache 2.X的捆绑吗?

Does anyone know of such directive in mod_deflate bundled with apache 2.x?

或有没有人知道如何COM preSS分块内容?

Or Does anyone know how to compress the chunked content?

推荐答案

其实我找到了解决办法。我用来创建GZipOutputStream的一个新的对象,每次刷新不同的块。相反,你应该创建一个对象只GZipOutputStream,然后使用该对象为COM pressing响应的所有块。另外,我把周围GZipOutputStream的包装。下面是我从Google上搜寻了周围的包装。

Actually I found the solution. I used to create a new object of GZipOutputStream each time to flush different chunks. Instead you should create one object only of GZipOutputStream and then used that object for compressing all the chunks of the response. Also I put a wrapper around GZipOutputStream. Here is the wrapper that I got from googling around.

public class GZIPFlushableOutputStream extends GZIPOutputStream {

    public GZIPFlushableOutputStream(final OutputStream out) throws IOException {
        // Using Deflater with nowrap == true will ommit headers and trailers
        super(out);
    }

    private static final byte[] EMPTYBYTEARRAY = new byte[0];

    /**
     * Insure all remaining data will be output.
     */
    public void flush() throws IOException {
        /**
         * Now this is tricky: We force the Deflater to flush its data by
         * switching compression level. As yet, a perplexingly simple workaround
         * for
         * 
         * http://developer.java.sun.com/developer/bugParade/bugs/42557 43.html
         */
        def.setInput(EMPTYBYTEARRAY, 0, 0);

        def.setLevel(Deflater.NO_COMPRESSION);
        deflate();

        def.setLevel(Deflater.DEFAULT_COMPRESSION);
        deflate();

        out.flush();
    }
}

这篇关于如何让Apache的mod_deflate模块和传输编码:分块一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆