immutable_types
The `immutable_types` directive specifies MIME types for which the `immutable` attribute will be applied to cache control headers.
Description
The immutable_types directive is an integral part of the NGINX module designed to optimize caching behavior for static assets. When enabled, it instructs NGINX to set the immutable attribute in the Cache-Control header specifically for the listed MIME types. This attribute signals to the browser that the response body will not change, allowing for better caching strategies. The response is thus considered stable, resulting in reduced requests to the server and improved loading times for clients.
This directive requires at least one argument, which is a list of valid MIME types. These types need to be specified in the configuration to ensure that the relevant assets are treated as immutable. When a browser encounters these types, it avoids confirming their freshness upon subsequent requests, which is especially useful for assets with versioning in their names (like hashed filenames), effectively reducing cache validation requests that don't alter the resource.
In configurations where immutable is applied, NGINX automatically sets far future expiration dates for these types when served over HTTP/1.0 or in the absence of Expires. This is essential for delivering a better user experience by leveraging cached content as much as possible without undue re-validation with the server.
Config Example
http {
server {
location /assets/ {
immutable on;
immutable_types image/png image/jpeg;
}
}
}Ensure that the MIME types specified are correct to avoid unintended caching behavior.
For browsers that do not support the immutable attribute, the effectiveness of this directive can be limited.
Misconfiguration could lead to stale content being served if the asset actually changes.