dynamic_etag

The `dynamic_etag` directive enables the automatic generation of ETag headers for dynamic content served by NGINX.

Syntaxdynamic_etag on | off | $var;
Defaultoff
Contexthttp, server, location
Arguments1

Description

The dynamic_etag directive is part of the NGINX module designed to add ETag headers to dynamically generated content. An ETag is a unique identifier assigned by the server to a specific version of a resource, allowing clients to make conditional requests and potentially reduce bandwidth usage by avoiding full responses for unchanged resources. This directive can be set in the http, server, or location contexts, which allows flexibility in defining its scope within the server configuration.

When enabled, the module computes an ETag by reading the entire response body and taking a hash of it. This approach is particularly useful for dynamic content generated by back-end scripts such as PHP, but it does introduce a caveat: reading the entire response may go against NGINX’s typical lightweight request processing model. Moreover, since the module relies on computing a hash of the response body, it will not generate ETags for HEAD requests as there is no body to hash, potentially leading to inconsistencies in ETag handling. Users should ensure that dynamic content appropriately benefits from ETags and that server responses do not change with each request.

Config Example

http {
    server {
        location ~ \.php$ {
            dynamic_etag on;
            fastcgi_pass ...;
        }
    }
}

ETags will not be returned for HEAD requests due to the absence of a response body.

Applying dynamic_etag to frequently changing dynamic content may not yield effective caching benefits.

Ensure that the response body is consistent and suitable for ETag generation to avoid issues with cached data.

Avoid using dynamic_etag with responses that change per request unless appropriate caching measures are applied.

← Back to all directives