Advanced configuration for mod_expires

The How to configure Apache Http Server to return cache-control, expires header has information about how you can use mod_expires module to set cache-control and Expires header. I wanted to learn details of how to set headers using mod_expires and this is what i found

The mod_expires has two directives that you can use to configure its behavior

  • ExpiresDefault: This directive sets the default algorithm for calculating the expiration time for all documents in the affected realm. It can be overridden on a type-by-type basis by the ExpiresByType directive

    ExpiresDefault "access plus 1 month"

    This directive tells that every resource served by the Apache will have expiration date of 1 month after the resource was accessed

  • ExpiresByType: This directive defines the value of the Expires header and the max-age directive of the Cache-Control header generated for documents of the specified type. Ex if you want to setup rule for gif images you can use expression like this

    ExpiresByType image/gif "access plus 3 month"

    This directive will override the expiration time for resources which return image/gif as content type and it will set expiration time of 3 months after the image is accessed by client

In both cases you have to use syntax like this for information on what should be the expiration date

ExpiresDefault "<base> [plus] { }*"

  • Base: Represents what should be the base for calculating expiration time or max-age value. Value of base can be either of following 2

    1. access/now: In this case the base will be when the resource is accessed by the client. Ex. if you want images to be cached for say 1 month from the time it is accessed by client then you can set it to access plus 1 month. So if as a user i access image on 1st Jan, it will set expiration date of 1st Feb but if i access same image on 20th of Jan it will set expiration date of 20th of Feb, so the expiration date will depend on the time when client access the image

    2. modification: Means the modification date of the resource on disk is considered as base. Ex. if your HTML changes everyday night at 12.00 PM and you want to cache it for 1 day you can configure to modification plus 1 day. So if the html was generated say on 1.00 AM today and if first user accesses it at 1.15 AM, it will send expiration date of 1.00 AM tomorrow, if second user accesses that image at 12 PM it will still set the same expiration date as that of the first user. SO the expiration date remains same for every user

  • type: value of type can be either of this

    1. years

    2. months

    3. weeks

    4. days

    5. hours

    6. minutes

    7. seconds

I changed my httpd.conf to use following configuration

ExpiresActive On
ExpiresDefault "access 1 month"
ExpiresByType image/gif "access plus 3 month"

So by default every resource will expire in 1 month but gif images will expire in 3 months. I tried accessing a html page which has an image and this is what i see

No comments: