Optimized mod_expires settings for Apache

1 feb 2024 2 min di lettura
Optimized mod_expires settings for Apache
Indice dei contenuti

Introduction

The mod_expires module in Apache is used to control the setting of the Expires HTTP header and the Cache-Control HTTP header max-age directive in server responses. These headers determine how long a client (browser or proxy) will cache resources. Optimizing these settings can significantly reduce server load and improve the client-side experience by reducing load times.

Optimized mod_expires settings

Here's a mod_expires section optimized for a production server, covering a variety of file types:

## EXPIRES CACHING ##
 <IfModule mod_expires.c>
 ExpiresActive On

 # Default expiration: 1 hour after request
 ExpiresDefault "now plus 1 hour"

 # HTML components
 ExpiresByType text/html "access plus 0 seconds"

 # Data interchange
 ExpiresByType application/json "access plus 0 seconds"
 ExpiresByType application/xml "access plus 0 seconds"
 ExpiresByType application/rss+xml "access plus 1 hour"
 ExpiresByType application/atom+xml "access plus 1 hour"

 # Favicon (can be cached for a long time)
 ExpiresByType image/x-icon "access plus 1 year"

 # Media: images, video, audio
 ExpiresByType image/gif "access plus 1 month"
 ExpiresByType image/png "access plus 1 month"
 ExpiresByType image/jpeg "access plus 1 month"
 ExpiresByType image/webp "access plus 1 month"
 ExpiresByType video/ogg "access plus 1 month"
 ExpiresByType audio/ogg "access plus 1 month"
 ExpiresByType video/mp4 "access plus 1 month"
 ExpiresByType video/webm "access plus 1 month"

 # CSS and JavaScript
 ExpiresByType text/css "access plus 1 year"
 ExpiresByType application/javascript "access plus 1 year"
 ExpiresByType application/x-javascript "access plus 1 year"
 ExpiresByType text/javascript "access plus 1 year"

 # Webfonts
 ExpiresByType font/otf "access plus 1 month"
 ExpiresByType font/ttf "access plus 1 month"
 ExpiresByType font/woff "access plus 1 month"
 ExpiresByType font/woff2 "access plus 1 month"
 ExpiresByType application/font-woff "access plus 1 month"
 ExpiresByType application/font-woff2 "access plus 1 month"
 ExpiresByType image/svg+xml "access plus 1 month"

 # Other
 ExpiresByType application/pdf "access plus 1 month"
 ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
 </IfModule>

Explanation of settings

  • ExpiresDefault - This is the default caching time for resources that don't match other rules. It's set to 1 hour, but you can change it based on how often the content changes.
  • HTML and data exchange: These types of content usually change frequently and are intended to expire immediately (0 seconds) or after a short period (1 hour for feeds).
  • Favicon: They rarely change and can be cached for a longer period (1 year).
  • Media files: Images, videos, and audio are typically not updated frequently. The expiration is expected in 1 month, but it is possible to increase it if these resources change rarely.
  • CSS and JavaScript: Because these files may change with website updates, but not as frequently as HTML content, they are set to have a longer cache period (1 year). Ensure version control of these files to avoid caching issues when they are updated.
  • Web Fonts: Fonts generally do not change once set, so a longer caching period (1 month) is appropriate.
  • Other types: For assets like PDFs and specific image types, adjust the caching time based on how often these assets are updated.

Additional notes

  • Change these settings based on your specific content update frequency.
  • Make sure you have a version control strategy for resources like CSS and JavaScript to avoid caching issues when these files are updated.
  • Note that aggressive caching may cause problems when content is updated on the server but is still cached in the client browser.
  • Regularly monitor and adapt these settings based on specific needs and feedback from your production environment.
Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.