Sam*_*hah 5 apache mod-deflate chunked
我正在尝试在我们的网站上使用大管道概念。这意味着尝试以块的形式发送响应,而不是整体发送响应,以便用户感觉该页面很快。我通过在 java 中的响应对象上使用flushBuffer方法成功地做到了这一点。但是现在,当我尝试使用 apache mod_deflate 模块压缩内容时,分块丢失了。
这是 apache 用于压缩内容的配置
**
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
Run Code Online (Sandbox Code Playgroud)
这是apache中打开deflate时的响应头
连接:保持活动
内容编码:gzip
内容长度:7916
内容类型:text/html;字符集 = UTF-8
日期:2012 年 1 月 27 日星期五 20:11:11 GMT
保持活动:超时 = 300,最大值 = 3997
服务器:Apache
Vary:Accept-Encoding
apache 中关闭 deflate 时的响应头
连接:保持活动
内容类型:text/html;字符集 = UTF-8
日期:2012 年 1 月 27 日星期五 20:21:14 GMT
保持活动:超时 = 300,最大值 = 3997
服务器:Apache/2.2.3 (CentOS)
传输编码:chunked
正如您在上面 2 个标头中看到的,只有在压缩关闭时分块才起作用。我在互联网上搜索有关此问题的信息,人们建议减小DeflateBufferSize值。我将值减少到 100 个字节,正如您在我的 apache 配置中看到的那样,但这仍然没有解决问题。DeflateBufferSize 设置为 100 字节意味着响应在 apache 中缓冲,直到接收到 100 字节,然后对其进行压缩。
我正在查看与旧的 apache 1.3 捆绑在一起的 mod_gzip 模块,该模块有一个以下指令,允许对分块内容进行 gzip 压缩。
mod_gzip_dechunk 是
有谁知道与 apache 2.x 捆绑在一起的 mod_deflate 中有这样的指令吗?
或者有人知道如何压缩分块内容吗?
事实上我找到了解决方案。我曾经每次创建一个新的 GZipOutputStream 对象来刷新不同的块。相反,您应该仅创建一个 GZipOutputStream 对象,然后使用该对象来压缩响应的所有块。我还对 GZipOutputStream 进行了包装。这是我通过谷歌搜索得到的包装。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7232 次 |
| 最近记录: |