$gzip_ratio

The $gzip_ratio variable holds the compression ratio achieved by gzip when NGINX serves a compressed response. — NGINX Core (HTTP)

$gzip_ratio NGINX Core (HTTP)

Description

The $gzip_ratio variable is a dynamic variable in NGINX that represents the ratio of the compressed size to the original size of the response when gzip compression is enabled. This variable is calculated at the time a response is served, specifically when the 'gzip' module is utilized to compress the output before it is sent to the client. The value is computed based on the size of the uncompressed response compared to the size of the compressed response, allowing for the evaluation of how effective the compression is. When the gzip module is enabled in NGINX, and a response is being compressed, the $gzip_ratio variable will be set after the compression completes. The typical output value can range from 1 to 100, with 100 indicating that the response was not compressed at all, and lower values indicating better compression efficiency. Developers can leverage this variable to monitor performance and tweak their gzip settings accordingly, ensuring optimal use of bandwidth and better load times for users. In instances where gzip cannot compress the response effectively, the variable may reflect a compression ratio of 100. This enables administrators to diagnose scenarios where certain types of content (like already compressed files - e.g., JPEG images) should not be handled by gzip settings, as they won't yield any benefits from further compression.

Config Example

gzip on;
gzip_min_length 1024;
set $compression_ratio $gzip_ratio;

Subsystem

http

Cacheable

Yes

Contexts

http, server, location

$gzip_ratio only reflects values when gzip compression is applied; if gzip is not enabled, it will not be set.

Using $gzip_ratio before gzip compression starts will yield empty or default values, causing potential miscalculations in logging or response handling.

Keep in mind that $gzip_ratio is only relevant for at least HTTP/1.1 clients, as older protocols may not support gzip negotiation.