0

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?

0

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
-1

You can make a location for these files and set default_type for them... But why are you doing such a strange thing?

It could be soemthing like this:

server {
  server_name my_server.com;
  listen 80;
  location /my_files { 
    types         { } 
    default_type  text/css;
 }   
}

Theoretically it should send all files under this location as "text/css" mime type. But I have never did this and have no idea will it work. Gzip could be enabled in the location too.

Anyway, more inromation could help.

  • This isn't an answer - you're asking a clarification to the OP, so post a comment under the question. – David Makogon Jun 21 '16 at 2:10
  • "But why are you doing such a strange thing?" I think you mean the combining of files without extension? Well that's part of the CMS which I can't easily alter. Complicating factor is that both JS and CSS files are in the directory. And by default they are already transfered as text/html and should be gzipped by default. But they are not. – stUrb Jun 21 '16 at 6:17
  • Ok, try to create nginx location for this directory, set the default_type there. Nginx uses types to find out what to gzip too nginx.org/en/docs/http/ngx_http_gzip_module.html I hope it will work together with the default_type – Dilyin Jun 21 '16 at 6:45
  • Nope. this is not working – stUrb Jun 21 '16 at 7:01
  • I've managed to make it work. – Dilyin Jun 21 '16 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.