Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Created February 16, 2011 13:19
Show Gist options
  • Save lukemorton/829351 to your computer and use it in GitHub Desktop.
Save lukemorton/829351 to your computer and use it in GitHub Desktop.
Apache config for nice caching
## By Luke Morton
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresActive On
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
# Force media to revalidate
<FilesMatch "\.(xml|txt|js|css|gif|jpe?g|png|swf)$">
Header append Cache-Control "must-revalidate"
</FilesMatch>
# Force no caching for dynamic files
<FilesMatch "\.(php|html?)$">
Header set Cache-Control "no-cache"
Header set Pragma "no-cache"
Header set Expires "-1"
</FilesMatch>
## Taken From http://httpd.apache.org/docs/2.0/mod/mod_deflate.html
## By Luke Morton
# Insert filter
SetOutputFilter DEFLATE
# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
@kornelski
Copy link

IMHO no-store is taking it too far — that's supposed to be for security-sensitive content. Also, must-revalidate also must kill off-line usage of content. max-age=0 (and Expires if you care about ancient clients) should be enough.

CSS could be publicly cacheable.

@lukemorton
Copy link
Author

So you're saying this is sufficient for what I wanted:
Header set Cache-Control "max-age=0"

@lukemorton
Copy link
Author

Updated to use mod_expires (stupid IE6).

@lukemorton
Copy link
Author

Added deflate.conf, but annoyingly I have an issue where IE6 is caching my <FilesMatch ".(php|html?)$"> unless I disable DEFLATE altogether.

@lukemorton
Copy link
Author

Updated so now deflate.conf and cache.conf work as expected. Tested in IE6-9, Firefox 3.6, Chrome 11.

@lukemorton
Copy link
Author

My latest changes in cache.conf are due to the required revalidation of media files. By forcing revalidation, the image will not be pulled from the server unless it has been updated, but the browser will always check if this is the case. YSlow will complain about the lack of an expires header, but in this cache, we want the modified header to enforce when the cache should be replenished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment