我正在使用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请帮我找出这里出了什么问题。当我在远程服务器中执行此设置时,也会发生同样的情况。
发布于 2015-10-06 09:03:41
我添加了值为false的属性useSendfile。
手册上说:
(Bool)使用此属性启用或禁用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" />费德勒信息:
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发布于 2016-08-01 07:21:09
在使用tomcat8开发angularjs应用程序时,我遇到了同样的问题。它让一些大型js files.Setting useSendfile对“false”有所帮助,从某种意义上说,有些文件被压缩了,但并不是所有文件都被压缩。在进一步的研究中,我发现有必要在compressableMimeType中添加'application/javascript‘。它适用于所有javascript文件。
来自tomcat8文档
该值是一个逗号分隔的MIME类型列表,可用于这些类型的HTTP压缩。默认值是text/html、text/xml、text/纯文本、text/css、text/javascript、application/javascript。
发布于 2018-09-07 23:59:50
任何试图在连接器上使用“force”属性的人都可以使用compression=FYI。只有当客户端支持压缩时,“强制”才是强制。
注意,在“内容编码”标题检查后,强制为.
/**
* 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;
}
...https://stackoverflow.com/questions/30608952
复制相似问题