A comprehensive guide to configuring caching and compression in Caddy for optimal performance.
Before configuring caching on Caddy, ensure you have:
example.com {
file_server
# Enable caching for static files
header {
Cache-Control "public, max-age=31536000"
}
# Cache different file types
@static {
path *.css *.js *.jpg *.jpeg *.png *.gif *.ico *.webp
}
header @static {
Cache-Control "public, max-age=31536000"
}
# HTML files with shorter cache
@html {
path *.html *.htm
}
header @html {
Cache-Control "public, max-age=3600"
}
}
Key directives explained:
example.com {
# Static assets with long cache
@assets {
path *.css *.js *.jpg *.jpeg *.png *.gif
}
header @assets {
Cache-Control "public, max-age=31536000, immutable"
}
# Dynamic content with no cache
@dynamic {
path /api/* /admin/*
}
header @dynamic {
Cache-Control "no-store, no-cache, must-revalidate"
Pragma "no-cache"
}
}
example.com {
encode gzip zstd
# Custom compression for specific types
@compress {
path *.html *.css *.js *.json *.xml *.svg
}
encode @compress gzip zstd {
minimum_length 1024
level 6
}
}
Compression settings explained:
# Caddy configuration will appear here