scgi_cache_valid

The 'scgi_cache_valid' directive defines the time period during which cached responses for SCGI requests are considered valid.

Syntaxscgi_cache_valid code time;
Defaultnone
Contexthttp, server, location
Arguments1+

Description

The 'scgi_cache_valid' directive is used to configure the caching behavior for SCGI (Simple Common Gateway Interface) responses based on their status codes. The directive allows an administrator to specify different caching durations for different HTTP response status codes, enhancing control over the caching mechanism. When a response is cached, the specified time (in seconds) indicates how long the response should be stored in the cache before it is marked as stale and requires a new request to be sent to the upstream service.

The directive can take one or more arguments, where each argument pairs an HTTP status code with a cache duration. For example, defining a cache period for successful responses (e.g., 200) may be different than those for error responses (e.g., 404). This flexibility helps in optimizing cache utilization and ensuring that users receive fresh content while still benefiting from the performance improvements of caching.

Moreover, 'scgi_cache_valid' is used within 'http', 'server', or 'location' blocks, allowing it to be tailored to specific routes or virtual hosts. It works in conjunction with the 'scgi_cache' directive, which must be enabled for caching to take effect. If a request's response matches a defined status code and has not exceeded the designated time, NGINX will deliver the cached version instead of making a new request to the backend, improving performance and reducing load.

Config Example

http {
    scgi_cache path/to/cache;
    scgi_cache_valid 200 1h;
    scgi_cache_valid 404 30m;
}

Ensure that 'scgi_cache' is enabled for caching to function properly.

Be cautious when setting long cache times for dynamic content, as it may serve stale data.

The directive does not work in isolation; it relies on complementary directives for full functionality.

← Back to all directives