proxy_cache_lock_timeout

Sets a timeout for acquiring a lock on a cached proxy response. — NGINX HTTP Core

proxy_cache_lock_timeout
httpserverlocation
Синтаксисproxy_cache_lock_timeout timeout;
По умолчаниюnone
Контекстhttp, server, location
МодульNGINX HTTP Core
Аргументы1

Описание

The `proxy_cache_lock_timeout` directive is used to specify the maximum time that NGINX will wait to acquire a lock on a response stored in the cache. This directive is useful in scenarios where multiple requests are attempting to retrieve the same content simultaneously. By utilizing a lock, NGINX can serialize these requests, allowing it to only perform the backend request once, while the other requests wait for the response to be cached. This behavior helps to reduce load on the upstream servers and improves overall performance. The value for `proxy_cache_lock_timeout` is specified in time format, such as `10s` for 10 seconds, and it can be set to zero to disable locking. When a request hits a cache miss, if a lock is available, the request will proceed to obtain the contents from the upstream server. If the lock is held by another request and the wait time exceeds the specified value, the new request will proceed to return a 502 error instead of waiting indefinitely. The directive can be declared within the `http`, `server`, and `location` contexts, providing flexibility in configurations where caching is required on a granular basis. It is essential to carefully choose the timeout value based on the application's response times and expected traffic patterns to avoid unnecessary request failures.

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

location /api {
    proxy_pass http://backend;
    proxy_cache my_cache;
    proxy_cache_lock on;
    proxy_cache_lock_timeout 10s;
}

Setting the timeout too low may lead to increased 502 Bad Gateway errors if the upstream server is slow to respond.

Make sure to enable `proxy_cache_lock` in conjunction for the locking mechanism to work properly.