proxy_cache_convert_head

The 'proxy_cache_convert_head' directive controls whether a HEAD request can return cached data for a GET request. — NGINX HTTP Core

proxy_cache_convert_head
httpserverlocation
Синтаксисproxy_cache_convert_head on | off;
По умолчаниюoff
Контекстhttp, server, location
МодульNGINX HTTP Core
Аргументыflag

Описание

The 'proxy_cache_convert_head' directive is used in NGINX configurations to determine if a HEAD HTTP request should serve a cached response based on a corresponding GET request. This directive enables the caching system to convert the HEAD request into a GET request internally if a valid cached version is available, thus enhancing performance by avoiding re-fetching the data from the upstream server every time a HEAD request is made. This behavior is particularly useful for applications that heavily utilize HEAD requests, allowing them to benefit from caching without requiring the upstream server to form a new response each time. When set to 'on', this feature alters the request handling, which may lead to quicker responses for HEAD requests. Conversely, if set to 'off', the server will not retrieve cached responses generated for GET requests for HEAD requests, potentially resulting in longer response times for HEAD requests, as they will need to hit the upstream server directly. The directive can be applied in various contexts including http, server, and location, providing flexibility in its use across different HTTP configurations within NGINX.

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

http {
    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m;
    server {
        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_convert_head on;
        }
    }
}

Ensure that the upstream server correctly handles GET and HEAD requests for the same resource.

Be aware that relying too much on cached HEAD responses may lead to stale data in certain cases.