Skip to main content
  1. Posts/

Nginx Caching Configurations

··77 words·1 min·
Table of Contents

Create directory for caching:

mkdir /var/cache/nginx

Edit /etc/nginx/conf.d/cache.conf:

# Cache config
proxy_cache_path /var/cache/nginx levels=1:2 use_temp_path=off keys_zone=cache:10m inactive=14d max_size=8G;

# Cached item is valid for 10 minutes
#proxy_cache_valid 10m;

# use proxy if upstream not working
#proxy_cache_use_stale error timeout updating http_502 http_503 http_504 http_429;

# Update in the background
#proxy_cache_revalidate on;
proxy_cache_background_update on;

#Enable caching
proxy_cache cache;

proxy_cache_lock on;
proxy_cache_lock_age 20s;
proxy_cache_lock_timeout 5s;

Cache-Control #

location ~* \.(jpg|jpeg|png|gif|ico|css|js|webp)$ {
    etag off;
    if_modified_since off;
    expires 30d;
    add_header Cache-Control "public";
}