Nginx and Gzip

I'm sure most other people know this already, but hey... maybe they don't?

Whilst nginx typically comes with gzip compression enabled by default, it doesn't compress proxied content by default. Since my default setup is to proxy to gunicorn, that means all of my Django content was going out uncompressed.

On top of that, the default compression level is 1... yes, it's fast, but how often is your site CPU bound?

Here's a comparison of compression times and rates for the page that triggered this effort. It's a django admin page with inlines, each of which has a very large list of m2m records to select from.

Timing is on my antiquated Toshiba Tecra S1 (Pentium-M 1.5G). I haven't done anything more than run it once for each, after doing an initial run to prime the caches/buffers.

LevelSizeTime
Original 505,128 N/A
gzip -1 61,678 0.038s
gzip -2 61,246 0.029s
gzip -3 55,777 0.029s
gzip -4 28,012 0.032s
gzip -5 25,684 0.032s
gzip -6 23,341 0.051s
gzip -7 23,245 0.062s
gzip -8 21,703 0.072s
gzip -9 21,665 0.079s
advdef -z4 21,110 1.644s

As you can see, there's a significant improvement in compression at level 4, without a major cost in time. After 5, it's a more linear time/size tradeoff.

So what does this mean? For general HTML content, level 4 at least for good results, 5 or 6 is probably safe... 9 only if you have a lot of large files, and CPU to spare.

comments powered by Disqus