Nginx not caching data

Solution 1:

You didn't told nginx for how much time the response is valid and must be served from cache.

This must be specified with proxy_cache_valid directive.

proxy_cache one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 10m;

But, this won't work for POST requests because you have no cache key that differs from a POST request to another on the same URL if they don't have same content.

So you will need to adjust the cache key to $host$request_uri|$request_body. You will have to monitor the cache size (proxy_cache_path parameter max_size) and proxy response buffer proxy_buffer_size so it suits your needs.

Solution 2:

From: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid

Syntax: proxy_cache_valid [code ...] time;

...

Parameters of caching can also be set directly in the response header. This has higher priority than setting of caching time using the directive.

  • The “X-Accel-Expires” header field sets caching time of a response in seconds. The zero value disables caching for a response. If the value starts with the @ prefix, it sets an absolute time in seconds since Epoch, up to which the response may be cached.
  • If the header does not include the “X-Accel-Expires” field, parameters of caching may be set in the header fields “Expires” or
    “Cache-Control”.
  • If the header includes the “Set-Cookie” field, such a response will not be cached.
  • If the header includes the “Vary” field with the special value “*”, such a response will not be cached (1.7.7). If the header includes
    the “Vary” field with another value, such a response will be cached
    taking into account the corresponding request header fields (1.7.7).

Processing of one or more of these response header fields can be disabled using the proxy_ignore_headers directive.

Most web apps set Set-Cookie header, so a response will not be cached. To fix that, use this directive:

proxy_ignore_headers Set-Cookie;