Static gzip

Ben Maurer bmaurer at andrew.cmu.edu
Sun Dec 23 00:54:20 MSK 2007


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







More information about the nginx mailing list