Vojtěch
Vojtěch

Reputation: 12426

Nginx: Correctly setting the mime type for gzipped styles/scripts

I have a gzip-compressed style and scripts in files:

/scripts.js.gz
/styles.css.gz

The problem is that it does not serve the correct mime-type and browser does not recognize, that the files are compressed css or js (browser recognize the type as application/octet-stream, where it should be text/css or so).

I tried adding the following to mime.type of nginx, but with no effect. I suppose, it does not recognize, that the file is compressed.

types {
    text/css                                css.gz;
    application/x-javascript                js.gz;
}

When trying to access the files, the browser handle the files as files to download and not to present.

Upvotes: 3

Views: 5442

Answers (2)

MichielB
MichielB

Reputation: 4295

I had the same problem, coming from Apache. The problem I found is, the types block does not allow you to specify .css.gz as a file extension. However, the location block does! My solution is to make a location for .css.gz and then modify the content type for .gz within that location, like this:

location ~ \.css\.gz$ {

   add_header  Content-Encoding  gzip;
   gzip off;
   types {
     text/css gz;
   }
}

Upvotes: 4

Dayo
Dayo

Reputation: 12785

Do not change the mime types and use the Gzip Static Module to handle this.

When this is active, Nginx will try to serve "file.ext.gz" first and then try "file.ext", if not found, for all requests within the context (location etc) where it is active.

Upvotes: 1

Related Questions