A comprehensive guide to configuring caching and compression in Lighttpd for optimal performance.
Before configuring caching on Lighttpd, ensure you have:
server.modules += ( "mod_expire" )
$HTTP["url"] =~ "\.(jpg|jpeg|png|gif|ico|css|js)$" {
expire.url = ( "" => "access plus 1 years" )
}
$HTTP["url"] =~ "\.(html|htm)$" {
expire.url = ( "" => "access plus 1 hours" )
}
# Enable mod_compress
server.modules += ( "mod_compress" )
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = (
"text/plain",
"text/html",
"text/css",
"text/javascript",
"application/javascript"
)
Key directives explained:
server.modules += ( "mod_setenv" )
# Static assets with long cache
$HTTP["url"] =~ "\.(css|js|jpg|jpeg|png|gif)$" {
setenv.add-response-header = (
"Cache-Control" => "public, max-age=31536000, immutable"
)
}
# Dynamic content with no cache
$HTTP["url"] =~ "^/(api|admin)/" {
setenv.add-response-header = (
"Cache-Control" => "no-store, no-cache, must-revalidate",
"Pragma" => "no-cache"
)
}
server.modules += ( "mod_compress" )
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = (
"text/plain",
"text/html",
"text/css",
"text/javascript",
"application/javascript",
"application/json",
"application/xml",
"image/svg+xml"
)
compress.allowed-encodings = ("gzip", "deflate")
compress.max-filesize = 1048576
Compression settings explained:
# Lighttpd configuration will appear here