HxHippy

Caching Configuration

Configuring Nginx caching for improved performance.

Last updated: 2025-01-15

Browser Caching (Static Files)

# Cache static assets
location ~* \.(jpg|jpeg|png|gif|ico|svg|webp)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
}

location ~* \.(css|js)$ {
    expires 7d;
    add_header Cache-Control "public";
}

location ~* \.(woff|woff2|ttf|eot|otf)$ {
    expires 365d;
    add_header Cache-Control "public, immutable";
}

Proxy Cache

Basic Setup

# In http context
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m
                 max_size=10g inactive=60m use_temp_path=off;

server {
    location / {
        proxy_pass http://backend;

        # Enable caching
        proxy_cache my_cache;
        proxy_cache_valid 200 60m;
        proxy_cache_valid 404 1m;

        # Add cache status header
        add_header X-Cache-Status $upstream_cache_status;
    }
}

Cache Parameters

proxy_cache_path /var/cache/nginx
    levels=1:2           # Directory structure
    keys_zone=cache:10m  # Name and key memory
    max_size=10g         # Maximum cache size
    inactive=60m         # Remove unused after 60m
    use_temp_path=off;   # Write directly to cache

Cache Key

# Default key
proxy_cache_key $scheme$proxy_host$request_uri;

# Include cookies
proxy_cache_key $scheme$proxy_host$request_uri$cookie_session;

# Include headers
proxy_cache_key $scheme$proxy_host$request_uri$http_accept_encoding;

Cache Control

location / {
    proxy_pass http://backend;
    proxy_cache my_cache;

    # Cache valid responses
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;
    proxy_cache_valid any 1m;

    # Don't cache if set-cookie
    proxy_no_cache $http_set_cookie;
    proxy_cache_bypass $http_cache_control;

    # Serve stale on errors
    proxy_cache_use_stale error timeout updating http_500 http_502 http_503;

    # Background update
    proxy_cache_background_update on;

    # Lock while updating
    proxy_cache_lock on;
    proxy_cache_lock_timeout 5s;
}

Bypass Cache

# Bypass for logged-in users
map $http_cookie $no_cache {
    default 0;
    "~*session" 1;
}

location / {
    proxy_cache my_cache;
    proxy_no_cache $no_cache;
    proxy_cache_bypass $no_cache;
}

Purge Cache

# Allow cache purge (requires ngx_cache_purge module)
location ~ /purge(/.*) {
    allow 127.0.0.1;
    deny all;
    proxy_cache_purge my_cache $scheme$proxy_host$1;
}
# Purge specific URL
curl -X PURGE https://example.com/purge/path/to/page

# Clear all cache manually
rm -rf /var/cache/nginx/*
nginx -s reload

FastCGI Cache

# In http context
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2
                   keys_zone=php_cache:10m max_size=1g inactive=60m;

server {
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;

        fastcgi_cache php_cache;
        fastcgi_cache_valid 200 60m;
        fastcgi_cache_key $scheme$request_method$host$request_uri;

        add_header X-FastCGI-Cache $upstream_cache_status;
    }
}
intermediate Performance Updated 2025-01-15
  • nginx
  • cache
  • proxy cache
  • performance
  • optimization