scgi_cache_key
The `scgi_cache_key` directive sets the cache key for SCGI responses in NGINX.
Description
The scgi_cache_key directive is used to specify a custom cache key for SCGI (pronounced 'sggi') responses in NGINX. By default, NGINX uses a generated key based on the request URI and other parameters to store response data in the SCGI cache. The ability to customize the cache key can optimize cache performance by ensuring that specific aspects of the request or variables can be used in the key generation, potentially reducing cache misses and improving response times.
This directive accepts a single argument, which can be a string or variable that defines how the key will be constructed. For example, you may choose to include specific parameters from the request or omit certain parts of the URI that aren't necessary for cache differentiation. By carefully crafting the cache key, users can optimize which responses are stored based on different criteria, leading to more effective use of cached content. It is important to note that since caching directly affects the performance and efficiency of your server, the key should be composed thoughtfully to avoid unintended cache collisions.
The scgi_cache_key directive is valid within the http, server, and location contexts, enabling its application across various levels of configuration. If left unspecified, NGINX will revert to its internal default mechanism for generating cache keys, which may not be as efficient for some use cases.
Config Example
location /api {
scgi_pass backend;
scgi_cache my_cache;
scgi_cache_key "$request_uri$request_method";
}Ensure the constructed key is unique enough to prevent cache collisions.
Beware of variable expansions; in certain cases, variables may not be available at the point of key generation.
Changing the cache key pattern will invalidate existing cached items.