Skip to content

HTTP Caching

naati implements proxy_cache, fastcgi_cache, uwsgi_cache, and scgi_cache with a lock-free shared-memory keys zone, stampede protection (*_cache_lock), background stale serving, conditional revalidation, Vary, and background cache manager & loader processes.

Production proxy_cache Example

http {
    proxy_cache_path /var/cache/naati/api
        levels=1:2
        keys_zone=api_cache:32m
        max_size=4g
        inactive=60m;

    server {
        listen 80;

        location / {
            proxy_pass http://app_backend;
            proxy_cache api_cache;
            proxy_cache_key "$scheme$request_method$host$request_uri";

            # Stampede protection
            proxy_cache_lock on;
            proxy_cache_lock_timeout 5s;

            # Serve stale on upstream errors or while updating
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            proxy_cache_background_update on;
            proxy_cache_revalidate on;

            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404      1m;

            add_header X-Cache-Status $upstream_cache_status always;
        }
    }
}
Cache directory note: Start naati with a fresh cache directory rather than reusing on-disk cache files written by NGINX. Temporary files are created inside the cache directory so renames remain atomic.