HTTP - Caching (Cache-Control Header)

> (World Wide) Web - (W3|WWW) > (HTTP|HTTPS) - Hypertext Transfer Protocol

1 - About

The Cache-Control headers (introduced in the HTTP 1.1 specification) cover browser caches and in most cases, intermediate caches as well:

  • Cache-Control: public - Any cache can store a copy of the content.
  • Cache-Control: private - Don't store, this is for a single user.
  • Cache-Control: no-cache - Re-validate before serving this content.
  • Cache-Control: no-store - Don't ever store this content.
  • Cache-Control: public, max-age=[seconds] - Caches can store this content for n seconds.
  • Cache-Control: s-maxage=[seconds] - Same as max-age but applies specifically to proxy caches.

Only the max-age, s-maxage, and private Cache-Control headers will influence proxy caching

Advertising

3 - Status Code

HTTP status code 304: response coming from the browser cache

4 - How

4.1 - Apache htaccess configuration

If the mod_header is present, Apache can send the caching header.

Example in the root htaccess, max age is in second

# Cache File
<IfModule mod_headers.c>
    # WEEK
    <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
        Header set Cache-Control "max-age=604800, public"
    </FilesMatch>
 
    # WEEK
    <FilesMatch "\.(js|css|swf)$">
        Header set Cache-Control "max-age=604800"
    </FilesMatch>
</IfModule>

5 - Specification

  • Caching (rfc7234), R. Fielding, M. Nottingham, J. Reschke. IETF.

6 - Documentation / Reference

Advertising
web/http/cache.txt · Last modified: 2019/05/24 20:11 by gerardnico