Tomcat8用于CSS,JS的Gzip压缩 [英] Tomcat8 Gzip Compression for CSS, JS

查看:731
本文介绍了Tomcat8用于CSS,JS的Gzip压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用tomcat8并试图模拟CSS和JS的GZIP压缩。我已经在server.xml中添加了条目,并按照

 < Connector port =8088protocol =HTTP / 1.1 
connectionTimeout =20000
redirectPort =8443compression =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 / javascriptsrc =extjs / ext-all-debug.js>< / script& 

但是在访问页面时,压缩不会发生,并且resposne标头收到如下。

 远程地址:[:: 1]:8088 
请求URL:http:// localhost:8088 / test / extjs / ext -all-debug.js
请求方法:GET
状态码:200 OK



< blockquote>

回应标题




 查看源代码
Accept-Ranges:bytes
Content-Length:4585183
Content-Type:application / javascript
日期:Wed,03 Jun 2015 00:34:12 GMT
ETag:W /4585183-1427778288000
Last-Modified:Tue,31 Mar 2015 05:04:48 GMT
服务器:Apache-Coyote / 1.1




请求标题




 查看源代码
接受:* / *
接受编码:gzip,deflate,sdch
Accept- 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

我在这里发现了什么问题。当我在远程服务器做这个设置也是一样。

解决方案

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



说:



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



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



我的连接器:

 < Connector port =8080protocol =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 
服务器:Apache-Coyote / 1.1
接受范围:字节
ETag:W /560012-1444044890000修改:Mon,05 Oct 2015 11:34:50 GMT
Content-Type:text / html
传输编码:chunked
Content-Encoding:gzip
Vary:Accept-编码
日期:Tue,06 Oct 2015 08:53:53 GMT


I am using tomcat8 and trying to simulate GZIP compression of CSS and JS. I have added the entry in server.xml and follows

 <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" />

And in my html page i have included the script as follows

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

But while accessing the page, compression is not happening and resposne header received as follows.

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

Response Headers

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

Request Headers

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

Please help me to find what went wrong here. The same is happening when i do this setting in remote server.

解决方案

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

这篇关于Tomcat8用于CSS,JS的Gzip压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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