NGINX Caching

Back to Production-Engineering/NGINX
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m
                 use_temp_path=off;

server {
    # ...
    location / {
        proxy_cache my_cache;
        proxy_cache_lock on;

        proxy_pass http://my_upstream;
    }
}

In location context:

Splitting a Cache

proxy_cache_path /path/to/hdd1 levels=1:2 keys_zone=my_cache_hdd1:10m max_size=10g 
                 inactive=60m use_temp_path=off;
proxy_cache_path /path/to/hdd2 levels=1:2 keys_zone=my_cache_hdd2:10m max_size=10g 
                 inactive=60m use_temp_path=off;

split_clients $request_uri $my_cache {
              50%          “my_cache_hdd1”;
              50%          “my_cache_hdd2”;
}

server {
    # ...
    location / {
        proxy_cache $my_cache;
        proxy_pass http://my_upstream;
    }
}

HTTP Headers

add_header X-Cache-Status $upstream_cache_status; adds the cache status in the response:

source