Nginx Cache
Today I learn nginx feature to cache the web response. Reference : http://nginx.org/en/docs/http/ngx_http_proxy_module.html
several cache config that I think useful are cache size, cache ttl, cache condition & key, cache background update, cache lock / singleflight, and header cache status information. combined it may looks like this
proxy_cache_path /var/cache/adibiarso levels=1:2 keys_zone=adibiarso_cache:10m max_size=200m inactive=10m use_temp_path=off;
location / {
proxy_cache adibiarso_cache;
proxy_cache_key "$request_uri";
proxy_cache_valid 200 404 1m;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://127.0.0.1:5000;
}
Breakdown
proxy_cache_path
option to set file cache location, directory level, max size, and inactive state
proxy_cache
option to set which cache path we use in case we have multiple location and multiple cache path.
proxy_cache_key
cache key to set in case we need to separate the cache key by specific request query or header
proxy_cache_valid
option to set the cache ttl
proxy_cache_use_stale
option to read stale cache if upstream return error
proxy_cache_background_update
obviously background update
proxy_cache_lock
option to activate cache lock or singleflight in case we get traffic spike at same time, we only allow 1 request reach the upstream while the other request will wait the response.
add_header
put cache status to response header to debug our response cache
Komentar
Posting Komentar