Mastering Nginx

(Ron) #1
Chapter 5

[ 113 ]

Directive Explanation
• no_last_modified:
Enables compression if the
response doesn't have a
Last-Modified header
• no_etag: Enables compression
if the response doesn't have an
ETag header
• auth: Enables compression
if the request contains an
Authorization header
• any: Enables compression for
any response whose request
includes the Via header
gzip_types The MIME types that should be compressed,
in addition to the default value text/html.
gzip_vary Enables or disables the response header
Vary: Accept-Encoding if gzip is active.

When gzip compression is enabled and you find large files being truncated, the likely
culprit is gzip_buffers. The default value of 32 4k or 16 8k buffers (depending on


the platform) leads to a total buffer size of 128 KB. This means that the file NGINX is
to compress cannot be larger than 128 KB. If you're using an unzipped large JavaScript


library, you may find yourself over this limit. If that is the case, just increase the


number of buffers so that the total buffer size is large enough to fit the whole file.


http {

gzip on;

gzip_min_length 1024;

gzip_buffers 40 4k;

gzip_comp_level 5;

gzip_types text/plain application/x-javascript application/json;

}

For example, the preceding configuration will enable compression of any file up to 40



  • 4 * 1024 = 163840 bytes (or 160 KB) large. We also use the gzip_min_length directive


to tell NGINX to only compress a file if it is larger than 1 KB. A gzip_comp_level of 4
or 5 is usually a good trade-off between speed and compressed file size. Measuring on


your hardware is the best way to find the right value for your configuration.

Free download pdf