How to configure Apache Http Server to return cache-control, expires header

The Apache HTTP Server has mod_expires, that you can use for setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses.

These HTTP headers are an instruction to the client about the document's validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered "expired" and invalid, and a new copy must be obtained from the source.


By default the mod_expires is turned off but you can turn it on and configure it by adding following instructions to your httpd.conf


ExpiresActive On
ExpiresDefault "access 1 week"


The ExpiresAction On will turn the mod_expires on but it does not do anything by itself, and you will have to set ExpiresDefault directive to let Apache no what should be the cache-control setting for every resource. In my case i am saying that every document would be fresh for 1 week after user's first access.

I tried accessing a static resource on Apache after configuration and this is what i see



The value of max-age is set to 604800 seconds which means 7 days. Also the Expires header is set by taking the value of Date header and adding 7 days to it.

The mod_expires sets both cache-control and Expires header because the html response might go through a proxy or other device that does not understand HTTP 1.0 and in that case that device will read value of Expires header.

No comments: