01 August 2010

Apache and cache improvements

Apache allows us to improve the caching of our resources. We need mod_expires and mod_headers. They must be loaded in (where?)

Example:
LoadModule expires_module libexec/mod_expires.so
LoadModule hearders_module libexec/mod_headers.so


AddModule mod_expires.c
AddModule mod_headers.c


AddModule mod_gzip.c

Note that mod_gzip must be loaded last.

To turn on mod_expires set:
ExpiresActive On

Then on the root directory:
Options FollowSymLinks MultiViews
AllowOverride none
Order allow, deny
Allow from all

ExpiresDefault A300

The ExpiresDefault directive tells us that it expires 300 seconds after the access. One day would be 86400 and 30 days A2592000

If we don't want to take advantage of caching we set:
Header Set Cache-Control "max-age=0, no-store"


# Not rely on file extensions
#
#
ExpireActive On
ExpiresDefault "access plus 300 seconds"


Options FollowSymLunks Multiviews
AllowOverride All
Order allow, deny
Allow from all
ExpiresByType text/html "access plus 1 day"
ExpiresByType text/css "access plus 1 day"
ExpiresByType text/javascript "access plus 1 day"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 day"



# We can minimize the response sent by setting
ServerTokens Min

Our images are now cachable for 30 days. However the HTML file does not have a Last-Modified header. This is because we use conditional server-side includes to merge in different CSS for different browsers to save a HTTP request. We'll address the cachability of SSI pages in a future tweak.

# The HTML file does not have a Last-Modified header. This is because we use
# conditional server-side includes to merge in different CSS for different
# browsers to save a HTTP request. We'll address the cachability of SSI pages in a future tweak.