Static gzip

Igor Sysoev is at rambler-co.ru
Wed Dec 26 17:10:53 MSK 2007


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

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

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


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list