Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX gzip not compressing JavaScript files

Tags:

nginx

All JavaScript files are not compressed by nginx gzip.

CSS files are working.

In my nginx.conf I have the following lines:

gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_buffers 16 8k;
gzip_types    text/plain application/x-javascript text/xml text/css;
gzip_vary on;
like image 440
user3633186 Avatar asked May 29 '14 17:05

user3633186


People also ask

Does nginx support gzip?

You can configure Nginx to use gzip to compress the files it serves on the fly. Those files are then decompressed by the browsers that support it upon retrieval with no loss whatsoever, but with the benefit of a smaller amount of data to transfer between the web server and browser.

Does nginx compress by default?

By default, NGINX does not compress responses to proxied requests (requests that come from the proxy server). The fact that a request comes from a proxy server is determined by the presence of the Via header field in the request. To configure compression of these responses, use the gzip_proxied directive.

How can I improve my gzip performance?

You will have a little bit of added programming to do in order to decode the gzip header and perform the integrity check with the gzip trailer. You would then use Inflater with the nowrap option to decompress the raw deflated data after then gzip header and before the trailer.


3 Answers

Change this line:

gzip_types    text/plain application/x-javascript text/xml text/css;

To be this:

gzip_types    text/plain application/javascript application/x-javascript text/javascript text/xml text/css;

Note the addition of application/javascript and text/javascript to your list of gzip types.

There are also more details—and a more expansive list of gzip types—in the answer posted here.

like image 105
Giacomo1968 Avatar answered Oct 06 '22 21:10

Giacomo1968


This is interesting, because the best-supported, old standard mime type for javascript in the browser is actually text/javascript. And if you configure that, in /etc/nginx/mime.types, it works.

text/javascript (Obsolete): JavaScript; Defined in and made obsolete in RFC 4329 in order to discourage its usage in favor of application/javascript. However, text/javascript is allowed in HTML 4 and 5 and, unlike application/javascript, has cross-browser support. The "type" attribute of the tag in HTML5 is optional and there is no need to use it at all since all browsers have always assumed the correct default (even in HTML 4 where it was required by the specification).

From this thread: text/javascript vs application/javascript

So the gzip module of nginx is simply built against previous standards, and apparently doesn't properly process the application/javascript mime type.

like image 35
Mauro Colella Avatar answered Oct 06 '22 23:10

Mauro Colella


Work for me (nginx):

gzip on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
    text/plain
    text/css
    text/js
    text/xml
    text/javascript
    application/javascript
    application/x-javascript
    application/json
    application/xml
    application/rss+xml
    image/svg+xml/javascript;
like image 3
iqmaker Avatar answered Oct 06 '22 21:10

iqmaker