Gziping static files

rmalayter nginx-forum at nginx.us
Thu Aug 11 13:50:25 UTC 2011

sarim Wrote:
> Ryan Malayter Wrote:
> --------------------------------------------------
> -----
> > nginx server on port 80/443 listening on
> > public-facing IP
> > proxy_cache enabled
> > gzip enabled
> >   |
> >   V
> > nginx server on localhost port 20080
> > gzip enabled
> >   |
> >   V
> > your back-end server
> Just tried to do that, but nginx is not caching
> gziped files. It caches in un-gziped state, so
> nginx gziping on every hit :(

The middle tier needs to have gzip enabled, as well as the front-end
tier. You will also need to enable gzip for HTTP 1.0 on the middle tier
using "gzip_http_version 1.0;" This is because nginx uses HTTP 1.0 to
talk with back-end servers (which in this case is nginx itself).
Finally, consider normalizing the gzip headers and adding it to your
proxy_cache_key at the frontmost layer, like so:

  #normalize all incoming accept-encoding headers to just gzip or empty
  #prevents caching of multiple versions of files based on differing
browser accept-encoding headers
  set $myae ""; #use empty string if accept-encoding does not contain
  if ($http_accept_encoding ~* gzip) {
    set $myae "gzip";
  location <whatever> {
    proxy_pass http://localhost:20080; #middle tier doing compression
    proxy_set_header Host $host;
    proxy_set_header Accept-Encoding $myae;
    proxy_cache zone1;
    proxy_cache_key "$request_uri $myae"; #use normalized accept
encoding as part of cache key

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,212423,213699#msg-213699

More information about the nginx mailing list