Lighttpd: Set Cache-Control: public, max-age Headers For Caching Purpose

To set Cache-Control: public, max-age headers for caching purposes in Lighttpd, you need to modify the configuration file. The default configuration file is usually located at /etc/lighttpd/lighttpd.conf.

To set the Cache-Control headers, add the following to your Lighttpd configuration file:

$HTTP["url"] =~ "\.(gif|jpg|jpeg|png|css|js)$" {
# set cache-control headers for static files
add_header "Cache-Control" "public, max-age=31536000, immutable";
}

This sets the Cache-Control headers to public, max-age=31536000, immutable for any URLs that end in .gif, .jpg, .jpeg, .png, .css, or .js. The max-age directive specifies the number of seconds that the resource should be considered fresh.

After making the changes, restart Lighttpd for the changes to take effect:

systemctl restart lighttpd

Note: The configuration file syntax may vary depending on the version of Lighttpd that you are using. Make sure to check the Lighttpd documentation for the correct syntax and additional options.

Leave a Comment