In Nginx, enabling gzip compression can significantly reduce the size of transmitted data (by half or even more), improve loading speed, and save server bandwidth.
Configuration Example
Add the following within the http block of the Nginx configuration file:
http {
gzip on; # Enable gzip compression
gzip_min_length 1k; # Only compress files larger than 1KB
gzip_buffers 4 16k; # Buffer size
gzip_comp_level 4; # Compression level
gzip_types text/plain text/css text/javascript text/html; # Specify file types to compress
gzip_vary on; # Add Vary: Accept-Encoding response header
}
A restart of Nginx is required for the changes to take effect.
Verify Compression is Working
Check if the response header contains Content-Encoding: gzip. Example using the curl command:
curl -H "Accept-Encoding: gzip" -I http://hostname
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Thu, 08 Jan 2026 10:31:53 GMT
Content-Type: text/html; charset=utf-8
Last-Modified: Thu, 18 Dec 2025 07:53:29 GMT
Connection: keep-alive
ETag: W/"6943b2f9-655"
Content-Encoding: gzip
Configuration Directives
Can be used within http, server, or location blocks.
It is recommended to set global defaults in the http block, adjust per site in the server block, and perform fine-tuning in the location block.
Enable or Disable gzip
Syntax: gzip on | off;
Default: gzip off;
Enable or disable gzip compression of response data.
Set Buffer Size gzip_buffers
Syntax: gzip_buffers number size;
Default: gzip_buffers 32 4k|16 8k;
number: The number of buffers. Minimum value is1.size: The size of each buffer, ink(KB) orm(MB). Usually set to align with the memory page size. By default, buffer size equals one memory page, which varies by platform.
Memory usage calculation:
gzip_buffers 32 4k;
# Per connection: 32 × 4KB = 128KB
# 100 concurrent connections: 100 × 128KB = 12.8MB
Set Compression Level gzip_comp_level
Syntax: gzip_comp_level level;
Default: gzip_comp_level 1;
level: Compression level, ranging from1to9. Higher values provide better compression but require more CPU resources.
Starting from level 6, increasing the compression level significantly raises CPU usage while offering diminishing returns in compression ratio improvement. It is recommended to use level 3 or 4 in production environments.
Disable Compression for Matching User-Agent gzip_disable
Syntax: gzip_disable regex ...;
Default: Not set
regex is a regular expression. Disables gzip compression for clients whose User-Agent matches the regex. For example:
# Gzip compression will be disabled in the following cases
gzip_disable "MSIE [1-6]\."; # IE6 and earlier versions
gzip_disable "Mobile"; # UAs containing "Mobile"
gzip_disable "Android 2\."; # Android 2.x
gzip_disable "curl"; # Disable for curl requests
Set Minimum HTTP Protocol Version gzip_http_version
Syntax: gzip_http_version 1.0 | 1.1;
Default: gzip_http_version 1.1;
Sets the minimum HTTP protocol version required to enable gzip compression.
Set Minimum File Size gzip_min_length
Syntax: gzip_min_length length;
Default: gzip_min_length 20;
Sets the minimum file size for enabling gzip compression. Example:
http {
gzip on;
# Do not compress files smaller than 1KB
gzip_min_length 1k;
}
Control Compression for Proxy Requests gzip_proxied
Syntax: gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
Default: gzip_proxied off;
Controls whether responses to proxy requests are gzip-compressed.
A request is considered a proxy request if the request header contains a Via field.
| Parameter | Description |
|---|---|
off |
Disable compression for all proxy requests. |
expired |
Enable compression if the response header contains an Expires field and it has expired. |
no-cache |
Enable compression if the response header contains Cache-Control: no-cache. |
no-store |
Enable compression if the response header contains Cache-Control: no-store. |
private |
Enable compression if the response header contains Cache-Control: private. |
no_last_modified |
Enable compression if the response header does not contain a Last-Modified field. |
no_etag |
Enable compression if the response header does not contain an ETag field. |
auth |
Enable compression if the response header contains an Authorization field. |
any |
Enable compression for all proxy requests. |
Specify MIME Types gzip_types
Syntax: gzip_types mime-type ...;
Default: gzip_types text/html;
Avoid compressing content that is already in a compressed format.
Specifies which MIME types of responses should be gzip-compressed. By default, only HTML is compressed. Example:
http {
gzip on;
# Compress common text types
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}
gzip_vary Directive
Syntax: gzip_vary on | off;
Default: gzip_vary off;
Adds Vary: Accept-Encoding to the response header.