1 Answer

0 votes
by
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:

gzip on;  

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.

gzip_types text/plain application/xml;  

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

gzip_min_length 1000;  

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.

location /storage/ {  

    gunzip on;  

    ...  

}  

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

server {  

    gzip on;  

    gzip_min_length 1000;  

    gunzip on;  

    ...  

}  

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.

location / {  

    gzip_static on;  

}  

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.

Related questions

0 votes
asked Sep 5, 2019 in NGINX by Robin
0 votes
asked Sep 5, 2019 in NGINX by Robin
...