if_modified_since

The 'if_modified_since' directive controls how NGINX responds to requests based on the Last-Modified timestamp of a specified resource.

Syntaxif_modified_since on | off | exact;
Defaultoff
Contexthttp, server, location
Arguments1

Description

The if_modified_since directive is used in NGINX to manage the caching of content based on the Last-Modified header sent by clients. This directive can take one of three values: off, on, or exact. When set to on, NGINX will check if a requested resource has been modified since the time specified in the If-Modified-Since header of the client's request. If the resource has not changed, NGINX responds with a 304 Not Modified status, indicating the client can use its cached version.

The exact option further refines the comparison to require an exact match of the timestamp. If the request's If-Modified-Since timestamp matches the Last-Modified timestamp of the resource, NGINX will return a 304 Not Modified response. This can be useful for stricter cache validation. Conversely, when set to off, the directive disables this checking behavior, and NGINX will always respond with the resource, regardless of the cached state on the client's side. This can be useful in scenarios where content updates are frequent and caching is not beneficial.

Config Example

server {
    location /images/ {
        if_modified_since on;
    }
}

Remember to check that your backend or static files set the Last-Modified header properly; otherwise, the directive won't work as expected.

The exact option will not return a 304 response if the timestamps do not match exactly, in contrast to the on option, which considers any non-modified resource as valid.

Ensure that your caching strategy works well with this directive to avoid unnecessary load on your server.

← Back to all directives