You can use lighttpd web-server to send the following and many other types of caching control headers:
public : Force caching for all static assets even if it would normally be non-cacheable.
max-age=number-of-seconds : Specifies the maximum amount of time that a representation will be considered fresh.
must-revalidate : Force the cache to obey any freshness information you give them about an object.
proxy-revalidate : Same as must-revalidate, except that it only applies to proxy caches.
All of the above directives affects the following web caches:
- Your web browser such as Firefox and others.
- Gateway and CDN networks such as Cloudfront, Akamai and others.
- Proxy server and reverse proxy servers such as squid, nginx, and others.
Example: mod_expire and mod_setenv
The mod_expire to set the Expire and Cache-Control: max-age headers in the Response Header of HTTP/1.0 and HTTP/1.1 messages under Lighttpd web server. The mod_setenv module allows influencing the environment external applications are spawned in and the response headers the server sends to the clients.
Edit lighttpd.conf file, enter:
# vi lighttpd.conf
Activate the mod_expire and mod_setenv:
server.modules = ( "mod_expire", "mod_setenv", "mod_redirect", "mod_rewrite", "mod_compress", "mod_access", "mod_auth", "mod_fastcgi", "mod_secdownload", "mod_accesslog", "mod_geoip" ) |

For example, make all images stored in /images/ expire 1 month after being accessed, by default:
expire.url = ( "/images/" => "access plus 1 month" ) |
The syntax is as follows:
<access|modification> plus <number> <years|months|days|hours|minutes|seconds> |
To make all images stored on server1.cyberciti.biz sub-domain expire 1000 days after being accessed, by default:
$HTTP["url"] =~ "^/" { expire.url = ( "" => "access 1000 days" ) } |
To add a public header to the HTTP request, enter:
setenv.add-response-header += ( "Cache-Control" => "public" ) |
To add both public and must-revalidate to the HTTP request, enter:
setenv.add-response-header += ( "Cache-Control" => "public, must-revalidate" ) |
Working example from my setup
### config for static assets by nixCraft for cyberciti.biz ### $HTTP["host"] == "s0.cyberciti.org"{ server.document-root = "/var/www/static-files" accesslog.filename = "/var/log/lighttpd/s0.cyberciti.org.log" $HTTP["url"] =~ "^/" { expire.url = ( "" => "access 5000 days" ) } setenv.add-response-header += ( "Cache-Control" => "public, must-revalidate, proxy-revalidate" ) } |
Save and close the file. Reload or restart the lighttpd web server:
# service lighttpd restart
OR
# service lighttpd reload
How do I see and test HTTP headers?
The syntax is:
curl -I http://static.example.com/images/file.png curl -I http://www.example.com/test.html |
In this example, examines HTTP resources for the url called http://www.cyberciti.biz/media/images/category/old/light_logo.png to find out how they will interact with Web caches:
curl -I http://s0.cyberciti.org/images/category/old/light_logo.png |
Sample outputs:
HTTP/1.0 200 OK Content-Type: image/png Content-Length: 8852 Connection: keep-alive Server: nginx Date: Fri, 09 Nov 2012 10:06:32 GMT X-Whom: l3-com-cyber Cache-Control: public, must-revalidate, proxy-revalidate, max-age=432000000 Expires: Sun, 19 Jul 2026 10:06:32 GMT Accept-Ranges: bytes ETag: "732144214" Last-Modified: Wed, 14 Jan 2009 18:47:39 GMT Age: 15 X-Cache: Hit from cloudfront
#1: Understanding the Date: Fri, 09 Nov 2012 10:06:32 GMT http web cache headers
- The date send by s0.cyberciti.org server i.e. server’s clock.
- The clock is used to calculate freshness of the recourses.
#2: Understanding the Last-Modified: Wed, 14 Jan 2009 18:47:39 GMT http web cache header
- The resource last changed almost 3.5+ years ago.
#3: Understanding the Cache-Control: public, must-revalidate, proxy-revalidate, max-age=432000000 http web cache header
- This server allows all caches to store this image file (public).
- This image file is fresh until 13.5+ years days from now (max-age=432000000).
- This image file cannot be served by a cache once it becomes stale (must-revalidate, proxy-revalidate).
#4: Understanding the Age: 15 http web cache header
- This image file has been cached for 15 seconds.