proxy_cache_key

The `proxy_cache_key` directive sets the key used for caching proxied responses. — NGINX HTTP Core

proxy_cache_key
httpserverlocation
Синтаксисproxy_cache_key key_string;
По умолчанию$scheme$request_method$host$request_uri
Контекстhttp, server, location
МодульNGINX HTTP Core
Аргументы1

Описание

The `proxy_cache_key` directive defines how NGINX constructs the cache key for responses stored in the proxy cache. By default, it combines the protocol, host, URI, and query string to create a unique cache identifier for each request. By customizing the cache key, you can modify caching behavior and allow for finer control over what gets cached. The key can use variables, which enables dynamic key generation based on request parameters, headers, or other contextual data. The syntax for `proxy_cache_key` allows you to define a custom key string which can consist of static text and variables. For example, you can include the `$request_uri`, `$remote_addr`, and other variables to differentiate cached content. This flexibility allows multiple versions of a response to be cached based on user sessions or other criteria, enhancing your web cache's capability. Note that changing the cache key requires a good understanding of the cached content and how users interact with it to avoid unnecessary cache misses or overwriting.

Пример конфига

http {
    proxy_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:10m;
    server {
        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_key "$scheme$request_method$host$request_uri";
        }
    }
}

Ensure that the custom cache key generated does not lead to excessive cache fragmentation, as overly granular keys can reduce cache hit rates.

If using variables in your cache key, be cautious of performance implications, as unnecessary complexity can add overhead to cache lookups.