gzip_vary
The `gzip_vary` directive controls whether the `Vary: Accept-Encoding` response header is included for gzipped content.
Description
The gzip_vary directive in NGINX is used to enable or disable the inclusion of the Vary: Accept-Encoding HTTP header in responses where gzip compression is applied. This header is important for caching behavior with proxies and CDNs, as it indicates that the response may vary based on the Accept-Encoding request header sent by the client. When gzip_vary is set to on, it ensures that user agents that support different content encodings will receive correctly cached versions of resources based on the encoding they support.
When using gzip_vary, if gzip is enabled and this directive is activated, NGINX will add the Vary: Accept-Encoding header to the response. This allows intermediate caching systems to know that the response content can vary depending on whether the client accepts gzip-encoded data or not. If set to off, this header will not be added, which may lead to undesired caching behaviors, particularly for users that do not request gzip compression.
This directive can be defined in various contexts including http, server, and location, making it flexible for various configurations. It’s quite useful when different client types may need differing representations of the same resource based on their encoding capabilities.
Config Example
http {
gzip on;
gzip_vary on;
}Ensure that your caching mechanism (e.g., proxy or CDN) recognizes the Vary header.
Setting gzip_vary to on without enabling gzip may lead to confusion, as the header will be sent without any effect.