【问题标题】:Tomcat8 Gzip Compression for CSS, JS用于 CSS、JS 的 Tomcat8 Gzip 压缩
【发布时间】:2015-06-03 00:42:46
【问题描述】:

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

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

【问题讨论】:

    标签: compression gzip tomcat8 http-compression


    【解决方案1】:

    我添加了值为 false 的属性 useSendfile。

    手册说:

    (bool)使用此属性来启用或禁用发送文件功能。默认值是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" />
    

    提琴手信息:

    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
    

    【讨论】:

      【解决方案2】:

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

      来自 tomcat8 文档

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

      【讨论】:

        【解决方案3】:

        仅供参考,供尝试在连接器上使用compression="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;
            }
            ...
        

        Source

        【讨论】:

          猜你喜欢
          • 2020-08-12
          • 1970-01-01
          • 2011-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-28
          相关资源
          最近更新 更多