proxy_cache
The 'proxy_cache' directive in NGINX enables caching of proxied content to improve response times and reduce server load. — NGINX HTTP Core
Описание
The 'proxy_cache' directive specifies the cache zone for storing cached responses from proxied servers. By defining a cache, NGINX can store the responses to avoid making frequent requests to the backend server, effectively speeding up response times for end-users and reducing load on the backend system. The directive can be used in the context of HTTP, server, and location blocks, allowing flexibility in implementing caching strategies across various parts of the configuration. The syntax for the 'proxy_cache' directive is as follows: 'proxy_cache zone;', where 'zone' is the name of a defined cache zone, which must be previously created using the 'proxy_cache_path' directive. This directive controls how long responses will be stored and can influence how subsequent requests are served—either from cache or forwarded to the backend. Additionally, other directives can be set to fine-tune cache behavior, such as 'proxy_cache_valid', which defines time periods for caching responses depending on their status codes, and 'proxy_cache_key', which customizes how cache keys are generated based on request parameters. It's also important to note that this directive alone does not configure caching behavior. It must be complemented by additional configurations pertaining to the cache zone, specific cache behaviors, and shared memory allocation, which determine how caching is managed and optimized within the NGINX server.
Пример конфига
http {
proxy_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:10m;
server {
location / {
proxy_pass http://backend;
proxy_cache my_cache;
}
}
}Ensure that the cache zone is defined using 'proxy_cache_path' before referencing it in 'proxy_cache'.
Using 'proxy_cache' without proper cache controls can lead to stale data being served to users.
Be cautious with cache keys as they can lead to unexpected behavior if not set properly with 'proxy_cache_key'.
Make sure the filesystem permissions allow NGINX to write to the directory specified in 'proxy_cache_path'.