Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat8 Gzip Compression for CSS, JS

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.

like image 450
Shahul Dhasthagir Avatar asked Jun 03 '15 00:06

Shahul Dhasthagir


People also ask

Can CSS be Gzipped?

You can create gzipped version of files (i.e. style. css. zip) but you rarely ever do that and the browser won't know what to do with it. On the web, gzipping is done directly by your server.

Does gzip improve performance?

Gzip is a fast and easy way to improve page speed performance while still delivering a high-quality experience to your users. See if your website supports gzip by running a free speed test, and sign up for a free trial for more insights into your website's performance.

What is gzip in JavaScript?

gzip-js is a pure JavaScript implementation of the GZIP file format. It uses the DEFLATE algorithm for compressing data. Please note that since this is a pure JavaScript implementation, it should NOT be used on the server for production code. It also does not comply 100% with the standard, yet.


2 Answers

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
like image 136
Rob Lassche Avatar answered Oct 21 '22 11:10

Rob Lassche


I came across the same problem while working on an angularjs application with tomcat8. It had some large js files.Setting useSendfile to 'false' helped partially, in the sense some files got compressed but not all of them. On further research I found that it is necessary to add 'application/javascript' also in the compressableMimeType. That worked for all the javascript files.

From tomcat8 documentation

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

like image 41
Arun MR Avatar answered Oct 21 '22 10:10

Arun MR