fastcgi_cache

The fastcgi_cache directive enables caching of responses from FastCGI servers to improve web application performance.

Syntaxfastcgi_cache zone;
Defaultnone
Contexthttp, server, location
Arguments1

Description

The fastcgi_cache directive specifies a shared memory zone used for storing FastCGI responses, effectively caching them to reduce server load and improve response times for frequently requested content. When the FastCGI server generates a response, this response can be cached based on the request parameters, allowing subsequent identical requests to be served from cache rather than generating a new response by querying the backend server again.

The directive requires an argument indicating the name of the cache zone that is previously defined using the fastcgi_cache_path directive. It can be utilized in various contexts such as the http, server, and location blocks, enabling granular control over how different locations handle caching. In conjunction with related directives like fastcgi_cache_key, administrators can determine what constitutes a unique cache entry, thereby tailoring the behavior of the caching mechanism to suit application needs.

Caching behavior is further influenced by associated directives such as fastcgi_cache_valid, which defines the time for which cached entries are considered valid, and fastcgi_cache_bypass, allowing dynamic control of when to bypass cache, ensuring that updated data can still be served when necessary.

Config Example

http {
    fastcgi_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:10m;
    server {
        location / {
            fastcgi_pass backend;
            fastcgi_cache my_cache;
            fastcgi_cache_valid 200 1h;
        }
    }
}

Ensure the cache zone is defined with the fastcgi_cache_path directive; otherwise, this directive will not work as expected.

Be cautious with cache invalidation; stale data may be served if not properly configured.

Check that the appropriate permissions are set for the directory used for the cache storage.

← Back to all directives