ajp_cache_methods

The `ajp_cache_methods` directive specifies the HTTP methods that can be cached when using the AJP protocol in NGINX.

Syntaxajp_cache_methods [GET | HEAD | POST];
Defaultajp_cache_methods GET HEAD;
Contexthttp, server, location
Arguments1+

Description

The ajp_cache_methods directive defines which HTTP request methods should be considered for caching responses in the context of the AJP protocol. By default, it caches responses for the GET and HEAD methods. If the user specifies ajp_cache_methods POST, caching will still occur for GET and HEAD requests, but responses for POST requests will also be cached if they meet the criteria defined by other caching directives. This directive is important because it allows fine-tuning of when and what responses are stored in the cache, thus optimizing performance and resource usage.

The directive can take multiple methods as arguments, allowing users to specify if they desire caching for GET, HEAD, and POST methods together. However, the caching behavior for GET and HEAD cannot be disabled. This ensures that the default behavior aligns with typical web server practices, where these two methods usually yield cacheable content. It is essential to use this directive carefully, especially with POST, which is often associated with operations that change server state, as it can lead to unintended consequences if responses are cached and reused improperly.

Config Example

http {
    upstream tomcats {
        server 127.0.0.1:8009;
        keepalive 10;
    }

    server {
        listen 80;

        location / {
            ajp_pass tomcats;
            ajp_cache_methods GET HEAD POST;
        }
    }
}

If only POST is specified, caching for GET and HEAD will still occur.

Caution should be taken when caching POST responses, as they might not be idempotent, potentially leading to incorrect application behavior.

Ensure that your caching policies align with the expected behaviors of your application, especially for state-changing requests.

← Back to all directives