proxy_cache_methods

The proxy_cache_methods directive specifies which HTTP methods are eligible for caching in the proxy cache. — NGINX HTTP Core

proxy_cache_methods
httpserverlocation
Синтаксисproxy_cache_methods method1 [method2 ...];
По умолчаниюGET HEAD
Контекстhttp, server, location
МодульNGINX HTTP Core
Аргументы1+

Описание

The proxy_cache_methods directive in NGINX is used to define which HTTP methods should be cached when handling requests through a proxied server. By default, only GET and HEAD methods are cached due to the typical usage patterns where these methods are safe for repeated requests without changing the resource state. However, applications may require specific caching behaviors for other methods such as POST or PUT, for example when implementing aggressive caching strategies or optimizing performance. This directive accepts one or more HTTP method names as arguments, including 'GET', 'HEAD', 'POST', 'PUT', etc. To enable caching for a non-default method, it must be explicitly listed as an argument to this directive. NGINX will filter the incoming requests based on the methods defined in proxy_cache_methods and cache the responses appropriately. Notably, it is essential to consider the implications of caching non-idempotent methods, as this can lead to unexpected behavior if not managed correctly. An important aspect of using this directive is its context flexibility, allowing it to be set in http, server, or location contexts. The settings can be fine-tuned at different levels depending on application requirements. Care should be taken when changing caching behavior for HTTP methods, as this might affect the application logic and resource interactions significantly.

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

location /api { 
    proxy_pass http://backend;
    proxy_cache_methods GET POST;
}

Caching non-idempotent methods (like POST) can lead to unexpected application behavior.

Ensure that the backend server supports caching for the specified methods, or the configuration might not have the desired effect.

Misconfigured caching for sensitive operations could unintentionally serve stale data.