apachecachingstatic-contentmod-expires

Apache not caching static content


I read the official caching guide of the latest Apache httpd version, but did not understand how to get a minimal caching setup for static content.

Googling around, I finally added these rules to my /etc/apache2/apache2.conf (I'm using Ubuntu):

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 5 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
</IfModule>

<IfModule mod_headers.c>
     Header set Cache-Control "public"
</IfModule>

FileETag None

Obviously, I already enabled expires, headers, cache modules.

When I try to access an image, a css or js file, I see 200 OK the first time, and 304 the next ones. So, I thought I was right... but Google Pagespeed (for example) still complains about files that are not cached.

Actually, I had some suspects that I'm missing something:


Solution

  • I absolutely need my cache to expire suddenly when the file is changed

    TWhen you use mod_expires to send an Expires header the client doesn't have to make sure the file is fresh and you can't force a change ever.

    If you drop mod_expires, your static files will have an ETAG and a last-modified-time which allows browsers to make sure the file hasn't changed (these are the 304 responses).

    You'll need to a) scrutinize the pagespeed messages more closely B) assess them against your requirement and C) look at real world traffic in your access log wrt 304s.

    You do not want mod_cache for static files.