COMPRESSION AND DECOMPRESSION in Nginx

This section describes how to configure compression or decompression of responses, as well as sending compressed files.

In This Section

  • Introduction
  • Enabling Compression
  • Enabling Decompression
  • Sending Compressed Files
  • Introduction

    Compressing responses often significantly reduces the size of transmitted data. However, since compression happens at runtime it can also add 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 (for example, by a proxied server).

    Enabling Compression

    To enable compression, include the gzip directive with the on parameter.
    gzip on;
    By default, NGINX compresses responses only with MIME type text/html. To compress responses with other MIME types, include the gzip_types directive and list the additional types.
    gzip_types text/plain application/xml;
    To specify the minimum length of the response to compress, use the gzip_min_lengthdirective. The default is 20 bytes (here adjusted to 1000):
    gzip_min_length 1000;
    By default, NGINX does not compress responses to proxied requests (requests that come from the proxy server). The fact that a request comes from a proxy server is determined by the presence of the Via header field in the request. To configure compression of these responses, use the gzip_proxied directive. The directive has a number of parameters specifying which kinds of proxied requests NGINX should compress. For example, it is reasonable to compress responses only to requests that will not be cached on the proxy server. For this purpose thegzip_proxied directive has parameters that instruct NGINX to check the Cache-Controlheader field in a response and compress the response if the value is no-cacheno-store, orprivate. In addition, you must include the expired parameter to check the value of theExpires header field. These parameters are set in the following example, along with the authparameter, which checks for the presence of the Authorization header field (an authorized response is specific to the end user and is not typically cached):
    gzip_proxied no-cache no-store private expired auth;
    As with most other directives, the directives that configure compression can be included in thehttp context or in a server or location configuration block.
    The overall configuration of gzip compression might look like this.
    server {
        gzip on;
        gzip_types      text/plain application/xml;
        gzip_proxied    no-cache no-store private expired auth;
        gzip_min_length 1000;
        ...
    }

    Enabling Decompression

    Some clients do not support responses with the gzip encoding method. At the same time, it might be desirable to store compressed data, or compress 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 specified in the same context as the gzip directive:
    server {
        gzip on;
        gzip_min_length 1000;
        gunzip on;
        ...
    }
    Note that 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 version of a file to the client instead of the regular one, set thegzip_static directive to on within the appropriate context.
    location / {
        gzip_static on;
    }
    In this case, 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 version of the file.
    Note that the gzip_static directive does not enable on-the-fly compression. It merely uses a file compressed beforehand by any compression tool. To compress content (and not only static content) at runtime, use the gzip directive.
    This directive is defined in a separate module that might not be included in an open source NGINX build by default.
Share on Google Plus

About Admin

Arun is a JAVA/J2EE developer and passionate about coding and managing technical team.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment