expires

The 'expires' directive controls the automatic setting of the 'Expires' and 'Cache-Control' HTTP headers for specified resources in NGINX.

Syntaxexpires time | epoch | max;
Defaultnone
Contexthttp, server, location, if in location
Arguments1-2

Description

The 'expires' directive is utilized to set expiration times for static resources served by NGINX. By defining how long a resource should be considered fresh, it aids in browser caching and optimizing load times by reducing the number of requests sent to the server. The directive accepts a time value as its argument, which can be specified in seconds (e.g., '30s'), minutes (e.g., '5m'), hours (e.g., '12h'), or days (e.g., '1d'). Additional options also exist whereby you can specify 'max' for indefinite expiration or 'epoch' to set it to a past date.

When set, the 'expires' directive will automatically generate the appropriate response headers for client requests. This can be configured in various contexts including 'http', 'server', and 'location'. Notably, it can also be used within 'if' statements inside a location block for more granular control. Caching policies can be tailored to different types of resources by leveraging multiple 'expires' directives across different contexts, thus ensuring that static files are efficiently served.

Config Example

location /images {
    expires 30d;
}

Using 'expires' in inappropriate contexts may result in unexpected behavior.

Not understanding the difference between 'expires' and 'cache-control' headers may lead to confusion in caching policy.

Make sure that 'expires' does not conflict with other caching configurations in NGINX.

← Back to all directives