Nginx – Turn on Gzip for combined JS or CSS files without file extension

gzipnginx

I'm trying to configure GZip on my nginx server. It works for files with an file-extension.

To make a decision what kind of file is served over the network, Nginx does not analyze the file contents … Instead, it just looks up the file extension to determine its MIME type

So when I have a combine css file without a file extension it doesn't know it needs to be gzipped and serves it plain.

Is there a way to let nginx know that everything served from a specified location always needs to be gzipped. With or without an file extension?

Best Answer

I've got it working:

    location /files {
      types         { } 
      default_type  text/css;

      gzip  on;
      gzip_min_length  1;
      gzip_proxied     any;
      gzip_types       text/css;
      gzip_disable "MSIE [1-6]\.(?!.*SV1)";
      gzip_comp_level  6;
    }

And test:

curl -v localhost/files/test1
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 80 (#0)
> GET /files/test1 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.6.3
< Date: Tue, 21 Jun 2016 07:23:17 GMT
< Content-Type: text/css
< Content-Length: 13
< Last-Modified: Tue, 21 Jun 2016 07:23:08 GMT
< Connection: keep-alive
< ETag: "5768eb5c-d"
< Accept-Ranges: bytes
< 
MY DATA HERE
* Connection #0 to host localhost left intact

File without extension was sent as text/css mime. Or what do you need? text/html?

Now test gzip:

curl -H "Accept-Encoding: gzip" localhost/files/test1 | gunzip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    33    0    33    0     0    217      0 --:--:-- --:--:-- --:--:--   218
MY DATA HERE