Static gzip

Hi,

I’m trying to gzip static files in nginx (without compressing the file
each time). I’d like to do this for two reasons:

  1. The files we’re serving are very frequently requested, we’d prefer
    the CPU savings of not having to compress the files
  2. We actually find that we get a good savings by using advdef (a gzip
    encoder that uses 7zip to increase the compression ratio).

So far, I’ve found that doing this works:

set $okstring “”;

      if ($http_accept_encoding ~ "gzip") {
  set $okstring "$okstring header";

}

if (-f $document_root/$document_uri.gz) {
set $okstring “$okstring file”;
}

if ($okstring = " header file") {
rewrite “^(.*).js$” “$1.js.gz”;
}

location ~ .*.js.gz {
types { text/javascript gz; }
add_header Content-Encoding gzip;
add_header Vary Accept-Encoding;
}

However, this isn’t the cleanest solution.

I was trying to find a good way to write a module that did this, but
couldn’t figure out a way to intercept the request when it had been
mapped to a physical path. What’s the best way to go about writing a
module that does this?

  • Ben

On Sat, Dec 22, 2007 at 04:54:20PM -0500, Ben Maurer wrote:

set $okstring “”;
rewrite “^(.*).js$” “$1.js.gz”;
I was trying to find a good way to write a module that did this, but
couldn’t figure out a way to intercept the request when it had been
mapped to a physical path. What’s the best way to go about writing a
module that does this?

It’s simple content module (like ngx_http_static_module), that should be
set just before ngx_http_static_module. It should look
$request_filename.gz
file. I will probably implement it soon.
They should be controlled via

  gzip_static  on;

and should take into account the already existent directives:

  gzip_http_version   ...
  gzip_proxied        ...
  gzip_vary           ...

On Wed, Dec 26, 2007 at 05:10:53PM +0300, Igor S. wrote:

So far, I’ve found that doing this works:

However, this isn’t the cleanest solution.

  gzip_static  on;

and should take into account the already existent directives:

  gzip_http_version   ...
  gzip_proxied        ...
  gzip_vary           ...

I’ve written the module, it will be in the next release.