fastcgi_cache_methods

The fastcgi_cache_methods directive specifies which HTTP methods should be cached by FastCGI.

Syntaxfastcgi_cache_methods GET | HEAD | POST | ...;
DefaultGET
Contexthttp, server, location
Arguments1+

Description

The fastcgi_cache_methods directive is used within an NGINX configuration to dictate which HTTP methods (such as GET or POST) will be cached when utilizing FastCGI. By default, NGINX caches responses to GET requests only, which is suitable for pages that do not change frequently and can be served directly from the cache. However, in some scenarios, applications may respond to POST requests that could benefit from caching to improve performance and reduce load on the backend server. This directive allows administrators to customize caching behavior according to the needs of their application.

The directive takes one or more HTTP methods as its parameter. For example, specifying "GET POST" allows both GET and POST requests to be cached. It’s essential to consider the nature of the request and the associated response; not all POST requests should be cached as they often involve state changes. Improper caching of such requests may lead to incorrect application behavior or stale content being served to users.

When implementing this directive, it can be placed in the http, server, or location context, enabling flexible caching configurations depending on the granularity of control required. If the specified methods do not align with the expected behavior of your application, it may inadvertently lead to performance bottlenecks or invalidated cache scenarios.

Config Example

location ~ \.php$ {
    fastcgi_pass   backend;
    fastcgi_cache  my_cache;
    fastcgi_cache_methods GET POST;
}

Be cautious when caching POST responses as they may change application state.

Consider the implications of caching sensitive data in responses, as it may lead to security issues.

Make sure that the backend can handle cached POST responses appropriately. Use separate cache keys if necessary.

← Back to all directives