expires snippets

How to set the Expires header with Apache 2 and mod_expires

Tagged expires, mod_expires, caching, header, apache  Languages apacheconf

First you need to enable the mod_expires module:

a2enmod expires

Next add this to your configuration:

ExpiresActive On
# Set Expires header to current time by default
ExpiresDefault A0

<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
  ExpiresDefault "access plus 30 days"
</FilesMatch>

<FilesMatch "\.(jpg|jpeg|png|gif|swf|bmp|)$">
  ExpiresDefault "access plus 7 days"
</FilesMatch>

<FilesMatch "\.(txt|xml|js|css)$">
  ExpiresDefault "access plus 1 day"
</FilesMatch>

Now restart Apache:

$ sudo /etc/init.d/apache2 force-reload

Check that the proper headers are set with Firebug, Yahoo YSlow or Google Page speed.

How to configure nginx to add Cache-Control and Expires headers to static files

Tagged cache-control, expires, nginx  Languages 
#
# Add expires header for static content
# images, fonts, css, javascript
#
location ~* \.(js|css|jpg|jpeg|gif|png|otf|eot|fft)$ {
  # If a file, add expires header
  if (-f $request_filename) {
    expires      7d; #max;
    add_header  Cache-Control public;
  } 

  # If not a file, pass on to Sinatra/Rails
  # Only needed if you want to let e.g. Dragonfly/ImageMagick process the request
  if (!-f $request_filename) {
    passenger_enabled on;
  }
}