Home » NGINX Compression and Decompression

NGINX Compression and Decompression

by Online Tutorials Library

NGINX Compression and Decompression

The compression process reduces the size of the transmitted data. However, since compression happens at runtime, it can also include considerable processing overhead which can negatively affect performance. Nginx performs compression before sending responses to clients but does not double compress responses that are already compressed.

Enabling Compression

To enable the compression, add the gzip directive with the on parameter:

By default, Nginx compresses responses only with text/html (MIME type). To compress responses with other MIME types, add the gzip_types directive and list the additional types.

We can define the minimum length of the response to compress, use the gzip_min_length directive. The default is 20 bytes.

Enabling Decompression

Some clients do not handle responses with the gzip encoding method. At the same time, it might be desirable to store compressed data or responses on the fly and store them in the cache. To successfully serve both clients that do and do not accept compressed data, NGINX can decompress data on the fly when sending it to the latter type of client.

To enable runtime decompression, use the gunzip directive.

The gunzip directive can be defined in the same context as the gzip directive:

Note: This directive is defined in a separate module that might not be included in an open-source NGINX build by default.

Sending Compressed Files

To send a compressed file to the client instead of the regular one, set the gzip_static directive to on within the appropriate context.

In the above example, to service, a request for /path/to/file, NGINX tries to find and send the file /path/to/file.gz. If the file doesn’t exist, or the client does not support gzip, NGINX sends the uncompressed file.

You may also like