0

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:

  • I didn't activate mod_cache or mod_cache_disk. Should I? What's the basic set of rules for doing so?
  • Why is it necessary to disable the ETag?
  • I absolutely need my cache to expire suddenly when the file is changed: ideally, the expiration time (for css, js) could be very long, let's say, two weeks, but if the file changes after one hour, the user should mandatory get the updated file! Is that behavior automatically managed by Apache?
Fabio B.
  • 8,375
  • 22
  • 96
  • 167

1 Answers1

0

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.

covener
  • 16,079
  • 2
  • 27
  • 40
  • Ok thank you but... If I don't use mod_expires.. What should I use instead? Default Apache installation didn't set caching headers out of the box. What's the minimal setup? – Fabio B. May 27 '15 at 16:56
  • If it set ETAG or Last-Modified-Time (as it will on all static files), clients can check that they have the latest and greatest with a conditional request. Nothing else allows you to force your files to change on an update. – covener May 27 '15 at 16:58
  • Ok. But the question is: how to enable that behavior in Apache? I already know the theory – Fabio B. May 27 '15 at 16:59