Tomcat8 Gzip压缩CSS和JS

6

我正在使用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> 

但是访问页面时,压缩并没有发生,并且收到的响应头如下:

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个回答

13

我添加了一个属性useSendfile,将其值设置为false。

手册上说:

(布尔值)使用此属性启用或禁用sendfile功能。默认值为true。请注意,使用sendfile将禁用Tomcat可能对响应执行的任何压缩。

我的tomcat8现在正在压缩一个大的html文件。

我的连接器:

<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 信息:

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

3
我在使用Tomcat8开发AngularJS应用时遇到了同样的问题。由于有一些较大的JS文件,设置useSendfile为“false”能够部分地解决问题,也就是说一些文件被压缩了,但并不是所有文件都被压缩了。经过进一步研究,我发现需要在compressableMimeType中添加“application/javascript”,才能够对所有的JavaScript文件进行压缩。
以下是Tomcat8文档中的相关内容:
值是一个逗号分隔的MIME类型列表,表示可以使用HTTP压缩的类型。默认值为text/html,text/xml,text/plain,text/css,text/javascript,application/javascript。

0

提醒:对于任何试图在连接器上使用compression="force"属性的人来说,需要知道"force"只有在客户端支持压缩时才会生效。

请注意,"force"是在检查"Content-Encoding"头之后才会生效...

 /**
 * 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;
    }
    ...

源代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接