srcache_fetch

The srcache_fetch directive configures caching behavior for upstream requests based on specified HTTP methods and cache keys in NGINX.

Syntaxsrcache_fetch method1 [method2 ...] cache_key;
Defaultnone
Contexthttp, server, location, if in location
Arguments2-3

Description

The srcache_fetch directive is an essential part of NGINX's transparent subrequest-based caching system, enabling the caching of responses from upstream servers. When this directive is applied within various contexts such as http, server, or location, it specifies which HTTP methods can trigger cache fetches. This directive takes 2 to 3 arguments that define the HTTP methods (GET, POST, etc.) and a cache key pattern which is utilized to retrieve cached data or store new responses based on incoming requests.

The first argument specifies the list of accepted HTTP methods that will trigger a cache fetch. This is often a comma-separated list that includes methods such as GET and HEAD. The second argument defines what cache key to use either directly or through a variable based on the request's URI and other parameters. An optional third argument may specify additional parameters or conditions for cache usage. When a matching key is found, NGINX bypasses upstream processing and delivers the cached response, significantly improving performance by reducing load on upstream servers.

Config Example

location /api {
    srcache_fetch GET /api/cache_key;
}

Ensure that the methods specified are supported by the backend services to avoid unexpected behavior.

If not configured properly, caching may lead to stale responses being served rather than fresh data.

Consider the implications of using methods like POST with caching, as they typically involve state-changing requests.

← Back to all directives