Tomcat8 Gzip Compression for CSS, JS

I am using tomcat8 and am trying to simulate GZIP compression for CSS and JS. I have added an entry to 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 like this

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

      

But when the page is accessed, no compression occurs and the resposne header is 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 find what went wrong. The same thing happens when I do this setting on the remote server.

+3


source to share


2 answers


I added a useSendfile attribute with a value of false.

The manual says:

(bool) Use this attribute to enable or disable the ability to send files. The default is correct. Note that using sendfile will disable any compression that Tomcat might perform in 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" />

      

Information about 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

      

+9


source


I faced the same problem while working on an angularjs app with tomcat8. He had some big js files. Setting useSendfile to "false" partially helped, in the sense that some files were compressed, but not all of them. On further research I found that it was necessary to add "application / javascript" to compressableMimeType as well. This worked for all javascript files.

From tomcat8 documentation



The value is a comma-separated list of MIME types for which HTTP compression can be used. The default is text / html, text / xml, text / plain, text / css, text / javascript, application / javascript.

+1


source







All Articles