nginx as cache proxy not caching anything

Make sure your backend does not return Set-Cookie header. If Nginx sees it, it disables caching.

If this is your case, the best option is to fix your backend. When fixing the backend is not an option, it's possible to instruct Nginx to ignore Set-Cookie header

proxy_ignore_headers "Set-Cookie";
proxy_hide_header "Set-Cookie";

See the documentation

proxy_ignore_header will ensure that the caching takes place. proxy_hide_header will ensure the Cookie payload is not included in the cached payload. This is important to avoid leaking cookies via the NGINX cache.


I would like to add that multiple configuration options and combinations can disable proxy caching in Nginx. Unfortunately this is poorly documented.

In my configuration I set proxy_buffering on and it enabled caching as expected.


after going through multiple answers and comments, i found this configuration finally works:

10m = 10mb key cache, max_size to 2GB, inactive=120m (refresh from source after 120minutes of inactive), use_temp_path=off (to reduce io)

proxy_cache_valid - cache status of 200 and 302 for 60 minutes

proxy_cache_path /tmp/cache levels=1:2 keys_zone=default_cache:10m max_size=2g
                 inactive=120m use_temp_path=off;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 60m;

server {
    listen       80;
    server_name  example.com;

    # https://www.nginx.com/blog/nginx-caching-guide
    location / {
        proxy_cache default_cache;
        proxy_buffering on;
        proxy_ignore_headers Expires;
        proxy_ignore_headers X-Accel-Expires;
        proxy_ignore_headers Cache-Control;
        proxy_ignore_headers Set-Cookie;

        proxy_hide_header X-Accel-Expires;
        proxy_hide_header Expires;
        proxy_hide_header Cache-Control;
        proxy_hide_header Pragma;

        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_pass http://ip-of-host:80;

        #set            $memcached_key "$uri?$args";
        #memcached_pass 127.0.0.1:11211;
        # error_page     404 502 504 = @fallback;
    }
}