Skip to main content

Enabling Compression

1. Enabling Gzip

Directive Values Description
gzip on | off Enables or disables gzip compression for responses. Default is off.
gzip_types Content-Type values Defines MIME types that should be compressed. Default includes common text-based types.
gzip_min_length Integer (bytes) Minimum response size to compress. Default is 20 bytes.
gzip_comp_level 1-9 Compression level (1 is fastest, 9 is best compression). Default is 5.

Example: Basic Gzip Configuration

server {
    listen 80;
    root /var/www/html;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
    gzip_min_length 1024;
    gzip_comp_level 6;

    location / {
        try_files $uri $uri/ =404;
    }
}
Note: Gzip compression is most effective for text-based content like HTML, CSS, JavaScript, and JSON. Binary files such as images or videos are not compressed by default.

2. Setting Gzip Compression Level

Directive Values Description
gzip_comp_level 1 to 9 Compression level (1 = fastest, 9 = best compression)

Example: Setting Gzip Compression Level

gzip on;
gzip_comp_level 6;
Note: Higher compression levels increase CPU usage. Balance between compression ratio and performance.

3. Configuring Gzip Types

Directive Values Description
gzip_types text/plain, text/css, application/json, application/javascript, application/xml, text/xml, text/html Defines MIME types to compress. Default includes common text-based content types.

Example: gzip_types configuration

server {
    listen 80;
    root /var/www/html;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript application/xml text/xml text/html;
}

4. Setting Minimum Length

Directive Values Description
gzip_min_length 1024 Minimum response size in bytes to compress. Default is 1024.

Example: Configuring gzip_min_length

server {
    listen 80;
    root /var/www;

    gzip on;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript;
}

5. Enabling Gzip Vary Header

Directive Values Description
gzip_vary on | off Enables sending the Vary: Accept-Encoding header to prevent caching issues with compressed content.

Example: Enabling Gzip Vary Header

server {
    listen 80;
    server_name example.com;

    gzip on;
    gzip_vary on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
}
Note: The Vary: Accept-Encoding header is essential when serving compressed content to ensure that proxies and browsers correctly handle cached versions of both compressed and uncompressed responses.

6. Configuring Gzip Buffers

Directive Values Description
gzip_buffers 4 4k | 8 8k Sets the number and size of buffers used for compressing responses. Default is 4 4k.

Example: Setting Gzip Buffers

gzip on;
gzip_buffers 4 8k;
Note: Increasing buffer size can improve compression efficiency but may increase memory usage.

7. Disabling Gzip for IE6

Directive Values Description
gzip on | off Enable or disable gzip compression. Default is off.
gzip_disable string Disables gzip compression for specific user agents. Can be used to exclude IE6.

Example: Disabling Gzip for IE6

server {
    listen 80;
    server_name example.com;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;

    # Disable gzip for IE6
    gzip_disable "MSIE [1-6]\.";
}
Note: IE6 has known issues with gzip-compressed content, particularly when handling certain headers. The regex "MSIE [1-6]\." matches Internet Explorer versions 1 through 6.

8. Using Gzip Static

Directive Values Description
gzip_static on | off Enables sending precompressed .gz files if they exist. Requires gzip to be enabled.
gzip_static always Sends precompressed files even if client does not support gzip.

Example: Enabling gzip_static

server {
    listen 80;
    root /var/www/html;

    location / {
        gzip_static on;
        gzip on;
        gzip_types text/plain text/css application/json application/javascript;
    }
}
Note: Precompressed files must be generated separately (e.g., using gzip command or build tools) and placed alongside original files.

9. Configuring Brotli Compression

Directive Values Description
brotli on | off Enables or disables brotli compression. Default is off.
brotli_types Content types to compress Defines MIME types that should be compressed. Default includes text/html, text/css, application/json, etc.
brotli_comp_level 1 - 11 Compression level (1 = fastest, 11 = best). Default is 6.
brotli_window 1k - 32k Window size for brotli compression. Default is 512k.
brotli_min_length Integer in bytes Minimum response size to compress. Default is 20 bytes.

Example: Basic Brotli Configuration

server {
    listen 80;
    root /var/www/html;

    brotli on;
    brotli_types text/plain text/css application/json application/javascript text/xml application/xml;
    brotli_comp_level 6;
    brotli_min_length 20;

    location / {
        try_files $uri $uri/ =404;
    }
}
Note: Brotli compression is supported in Nginx 1.14.0+ and requires the ngx_brotli module to be compiled in.

10. Setting Brotli Quality

Directive Values Description
brotli on | off Enables or disables Brotli compression
brotli_comp_level 1 - 11 Compression level (1 = fastest, 11 = best compression)
brotli_window 10 - 24 Window size in bits (larger = better compression, more memory)

Example: Brotli Compression Configuration

server {
    listen 80;
    server_name example.com;

    brotli on;
    brotli_comp_level 6;
    brotli_window 512k;

    location / {
        root /var/www;
        index index.html;
    }
}

11. Configuring Brotli Types

Directive Values Description
brotli_types text/html Defines MIME types to compress using Brotli

Example: Brotli Types Configuration

location / {
    brotli on;
    brotli_types text/html text/css application/json application/javascript;
}

12. Understanding Compression Trade-offs

Aspect Description Impact
CPU Usage Compression requires CPU cycles to process content Higher compression levels increase CPU load, potentially affecting performance
Bandwidth Savings Compressed content reduces data size over the network Significant reduction in bandwidth usage, especially for text-based content
Latency Compression adds processing time before content is sent Can increase initial response time, but reduces overall transfer time
Content Type Text-based content (HTML, CSS, JS) benefits more from compression Binary files (images, videos) often do not compress well or at all
Client Support Most modern browsers support gzip, deflate, and brotli Older clients may not support compression, leading to fallback behavior

Compression Algorithms Comparison

  • Gzip: Widely supported, good balance of compression ratio and speed
  • Brotli: Better compression ratio, higher CPU cost, newer standard
  • Deflate: Older standard, less common, limited browser support
Note: Brotli offers better compression ratios than gzip but requires more CPU resources. For high-traffic sites, the trade-off between CPU usage and bandwidth savings should be evaluated.

Was this article helpful?