Tomcat 8.5.29 HTTP/2不支持GZIP压缩 [英] Tomcat 8.5.29 HTTP/2 is not supporting GZIP compression

查看:196
本文介绍了Tomcat 8.5.29 HTTP/2不支持GZIP压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tomcat 8.5.29,并使用各自的配置,因此已启用该站点的HTTP2支持.下面是server.xml文件中的配置.

I am using Tomcat 8.5.29 and using the respective configuration,I have enabled the HTTP2 support for the site. Below is the configuration in server.xml file.

<Connector port="443" protocol="org.apache.coyote.http11.Http11AprProtocol"
           maxThreads="150" SSLEnabled="true" compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json" compression="on" compressionMinSize="1024"
           >
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    <SSLHostConfig>
        <Certificate certificateKeyFile="conf/localhost-key.pem"
                     certificateFile="conf/localhost-cert.pem"
                     certificateChainFile="conf/cacert.pem"
                     type="RSA" />
    </SSLHostConfig>
</Connector>

当我尝试比较支持HTTPS 1.1和HTTP2的站点的页面加载时间时,不一致.与HTTPS 1.1相比,有时需要花费更多的时间来加载,有时需要花费较少的时间来加载.

When i tried to compare the page load time for the site which is supporting HTTPS 1.1 and HTTP2, it is not consistent. Sometime it is taking more time to load and sometime it is taking less time to load compare to HTTPS 1.1.

要测量页面加载时间,我正在使用Chrome浏览器中的开发者工具.

To measure the page load time i am using Developer tools from Chrome Browser.

后来,我发现HTTP/2的加载时间更长,因为响应中的内容编码"不是gzip.使用HTTP/1.1时,脚本文件的大小为4 MB(gzip),其中与HTTP/2一样,同一文件的大小为8 MB(无内容编码).压缩未发生.我们还尝试过将useSendfile和压缩作为"force"使用,但是它不起作用.

Later, I have found that Load time for HTTP/2 is higher because "content-encoding" in response is not gzip. Using HTTP/1.1, a script file size is 4 MB ( gzip) where as with HTTP/2, same file is of 8 MB ( no content-encoding). Compression is not happening. We have also tried with useSendfile and compression as "force" but it did not work.

我们还尝试了Tomcat 8.5.31版,但没有运气!面临同样的问题.

We have also tried with Tomcat version 8.5.31 but no luck !! Facing same issue.

我们已经使用了Tomcat 9,并使用以下配置,将内容压缩为gzip.现在,通过快速页面加载,文件大小减少了2 MB.

We have used Tomcat version 9 and using following configuration, got content in gzip (compressed). Now file size got reduced by 2 MB with a quick page load.

<Connector port="443" protocol="org.apache.coyote.http11.Http11AprProtocol"
           maxThreads="150" SSLEnabled="true" 
           >
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json" compression="on" compressionMinSize="1024" />
    <SSLHostConfig>
        <Certificate certificateKeyFile="conf/localhost-key.pem"
                     certificateFile="conf/localhost-cert.pem"
                     certificateChainFile="conf/cacert.pem"
                     type="RSA" />
    </SSLHostConfig>
</Connector>

看起来Tomcat 8.5版不支持HTTP2的gzip.有什么想法吗?

It looks like Tomcat version 8.5 is not supporting gzip with HTTP2. Any idea on this?

推荐答案

根据

注意:在使用压缩(节省带宽)和使用sendfile功能(节省CPU周期)之间需要权衡.如果连接器支持sendfile功能,例如NIO2连接器(使用sendfile)将优先于压缩.症状是大于48 Kb的静态文件将以未压缩的形式发送.您可以通过设置协议的useSendfile属性来关闭sendfile,如下所述,或者在默认conf/web.xml或Web应用程序的web.xml中的DefaultServlet的配置中更改sendfile使用阈值.

因此,请尝试将useSendfile ="false"添加到您的UpgradeProtocol设置中

So try adding useSendfile="false" to your UpgradeProtocol setting

<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json" compression="on" compressionMinSize="1024" useSendfile="false" />

请注意,此选项未出现在 8.5文档中,所以不确定是否为此存在(有参考文献您可以通过设置协议的useSendfile属性来关闭sendfile,如下所述",但在下面没有任何说明),尽管您也可以在服务器级别上将其关闭

Note this option does not appear in the 8.5 documentation so not sure if it exists for that (there is a reference to it "You can turn off sendfile by setting useSendfile attribute of the protocol, as documented below" but then nothing below), though you can also turn off at a server level.

很奇怪,在不关闭此选项的情况下,我也不会希望它在Tomcat 9中起作用,但是您说可以吗?您确定尚未关闭此功能吗?还是您正在使用小于48KB的文件进行测试?

Weirdly I wouldn't have expected this to work in Tomcat 9 either without turning off this option, but you say it does? Are you sure you have not turned this off? Or are you testing with a smaller than 48KB file on that?

这篇关于Tomcat 8.5.29 HTTP/2不支持GZIP压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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