用于CSS的Tomcat8 Gzip压缩,JS

时间:2015-06-03 00:42:47

标签: compression gzip tomcat8 http-compression

我正在使用tomcat8并试图模拟CSS和JS的GZIP压缩。我在server.xml中添加了条目,然后是

 <Connector port="8088" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" compression="on"
     compressionMinSize="2048"
     noCompressionUserAgents="gozilla, traviata"
     compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json" />

在我的html页面中,我已将脚本包含在内,如下所示

<script type="text/javascript" src="extjs/ext-all-debug.js"></script> 

但是在访问页面时,压缩没有发生,并且resposne header收到如下。

Remote Address:[::1]:8088
Request URL:http://localhost:8088/test/extjs/ext-all-debug.js
Request Method:GET
Status Code:200 OK
  

响应标题

view source
Accept-Ranges:bytes
Content-Length:4585183
Content-Type:application/javascript
Date:Wed, 03 Jun 2015 00:34:12 GMT
ETag:W/"4585183-1427778288000"
Last-Modified:Tue, 31 Mar 2015 05:04:48 GMT
Server:Apache-Coyote/1.1
  

请求标题

view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:localhost:8088
Referer:http://localhost:8088/test/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36

请帮我找一下这里出了什么问题。当我在远程服务器中执行此设置时,也会发生同样的情况。

3 个答案:

答案 0 :(得分:12)

I added attribute useSendfile with value false.

The manual says:

(bool)Use this attribute to enable or disable sendfile capability. The default value is true. Note that the use of sendfile will disable any compression that Tomcat may otherwise have performed on the response.

My tomcat8 is now compressing a large html-file.

My connector:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
            useSendfile="false"
            compression="on"
            compressionMinSize="2048"
            noCompressionUserAgents="gozilla, traviata"
            compressableMimeType="text/html,text/xml,text/plain,text/css"
           redirectPort="8443" />

Fiddler info:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"560012-1444044890000"
Last-Modified: Mon, 05 Oct 2015 11:34:50 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Tue, 06 Oct 2015 08:53:53 GMT

答案 1 :(得分:2)

我在使用tomcat8处理angularjs应用程序时遇到了同样的问题。它有一些大的js文件。将useSend文件设置为'false'部分帮助,在某种意义上,一些文件被压缩但不是全部。在进一步的研究中,我发现有必要在compressableMimeType中添加'application / javascript'。这适用于所有的javascript文件。

来自tomcat8文档

  

该值是以逗号分隔的MIME类型列表,可以使用HTTP压缩。默认值为text / html,text / xml,text / plain,text / css,text / javascript,application / javascript。

答案 2 :(得分:0)

仅供参考,适用于尝试在连接器上使用compression =“ force”属性的任何人。 “强制”仅是 客户端支持压缩的强制。

请注意,强制是在 标题检查之后的。

 /**
 * Check if the resource could be compressed, if the client supports it.
 */
private boolean isCompressible() {

    // Check if content is not already compressed
    MessageBytes contentEncodingMB = response.getMimeHeaders().getValue("Content-Encoding");

    if ((contentEncodingMB != null) &&
            (contentEncodingMB.indexOf("gzip") != -1 ||
                    contentEncodingMB.indexOf("br") != -1)) {
        return false;
    }

    // If force mode, always compress (test purposes only)
    if (compressionLevel == 2) {
        return true;
    }
    ...

Source