Apache HTTP Server Version 2.4
This document supplements the mod_cache
,
mod_cache_disk
, mod_file_cache
and htcacheclean reference documentation.
It describes how to use the Apache HTTP Server's caching features to accelerate web and
proxy serving, while avoiding common problems and misconfigurations.
The Apache HTTP server offers a range of caching features that are designed to improve the performance of the server in various ways.
mod_cache
and its provider modules
mod_cache_disk
provide intelligent, HTTP-aware caching. The content itself is stored
in the cache, and mod_cache aims to honor all of the various HTTP
headers and options that control the cacheability of content
as described in
Section
13 of RFC2616.
mod_cache
is aimed at both simple and complex caching configurations, where
you are dealing with proxied content, dynamic local content or
have a need to speed up access to local files on a potentially
slow disk.
mod_file_cache
offers the ability to pre-load
files into memory on server startup, and can improve access
times and save file handles on files that are accessed often,
as there is no need to go to disk on each request.
To get the most from this document, you should be familiar with the basics of HTTP, and have read the Users' Guides to Mapping URLs to the Filesystem and Content negotiation.
Related Modules | Related Directives |
---|---|
The HTTP protocol contains built in support for an in-line caching
mechanism
described by section 13 of RFC2616, and the
mod_cache
module can be used to take advantage of
this.
Unlike a simple two state key/value cache where the content disappears completely when no longer fresh, an HTTP cache includes a mechanism to retain stale content, and to ask the origin server whether this stale content has changed and if not, make it fresh again.
An entry in an HTTP cache exists in one of three states:
If the content is too old (older than its freshness lifetime), it is considered stale. An HTTP cache should contact the origin server and check whether the content is still fresh before serving stale content to a client. The origin server will either respond with replacement content if not still valid, or ideally, the origin server will respond with a code to tell the cache the content is still fresh, without the need to generate or send the content again. The content becomes fresh again and the cycle continues.
The HTTP protocol does allow the cache to serve stale data
under certain circumstances, such as when an attempt to freshen
the data with an origin server has failed with a 5xx error, or
when another request is already in the process of freshening
the given entry. In these cases a Warning
header
is added to the response.
Full details of how HTTP caching works can be found in Section 13 of RFC2616.
The mod_cache
module hooks into the server in two
possible places depending on the value of the
CacheQuickHandler
directive:
This phase happens very early on during the request processing, just after the request has been parsed. If the content is found within the cache, it is served immediately and almost all request processing is bypassed.
In this scenario, the cache behaves as if it has been "bolted on" to the front of the server.
This mode offers the best performance, as the majority of server processing is bypassed. This mode however also bypasses the authentication and authorization phases of server processing, so this mode should be chosen with care when this is important.
Requests with an "Authorization" header (for example, HTTP Basic
Authentication) are neither cacheable nor served from the cache
when mod_cache
is running in this phase.
This phase happens late in the request processing, after all the request phases have completed.
In this scenario, the cache behaves as if it has been "bolted on" to the back of the server.
This mode offers the most flexibility, as the potential exists for caching to occur at a precisely controlled point in the filter chain, and cached content can be filtered or personalized before being sent to the client.
If the URL is not found within the cache, mod_cache
will add a filter to the filter stack in order
to record the response to the cache, and then stand down, allowing normal
request processing to continue. If the content is determined to be
cacheable, the content will be saved to the cache for future serving,
otherwise the content will be ignored.
If the content found within the cache is stale, the
mod_cache
module converts the request into a
conditional request. If the origin server responds with
a normal response, the normal response is cached, replacing the content
already cached. If the origin server responds with a 304 Not Modified
response, the content is marked as fresh again, and the cached content
is served by the filter instead of saving it.
When a virtual host is known by one of many different server aliases,
ensuring that UseCanonicalName
is
set to On
can dramatically improve the ratio of cache hits.
This is because the hostname of the virtual-host serving the content is
used within the cache key. With the setting set to On
virtual-hosts with multiple server names or aliases will not produce
differently cached entities, and instead content will be cached as
per the canonical hostname.
Well formed content that is intended to be cached should declare an
explicit freshness lifetime with the Cache-Control
header's max-age
or s-maxage
fields, or
by including an Expires
header.
At the same time, the origin server defined freshness lifetime can
be overridden by a client when the client presents their own
Cache-Control
header within the request. In this case,
the lowest freshness lifetime between request and response wins.
When this freshness lifetime is missing from the request or the
response, a default freshness lifetime is applied. The default
freshness lifetime for cached entities is one hour, however
this can be easily over-ridden by using the CacheDefaultExpire
directive.
If a response does not include an Expires
header but does
include a Last-Modified
header, mod_cache
can infer a freshness lifetime based on a heuristic, which can be
controlled through the use of the CacheLastModifiedFactor
directive.
For local content, or for remote content that does not define its own
Expires
header, mod_expires
may be used to
fine-tune the freshness lifetime by adding max-age
and
Expires
.
The maximum freshness lifetime may also be controlled by using the
CacheMaxExpire
.
When content expires from the cache and becomes stale, rather than pass on the original request, httpd will modify the request to make it conditional instead.
When an ETag
header exists in the original cached
response, mod_cache
will add an
If-None-Match
header to the request to the origin server.
When a Last-Modified
header exists in the original
cached response, mod_cache
will add an
If-Modified-Since
header to the request to the origin
server. Performing either of these actions makes the request
conditional.
When a conditional request is received by an origin server, the origin server should check whether the ETag or the Last-Modified parameter has changed, as appropriate for the request. If not, the origin should respond with a terse "304 Not Modified" response. This signals to the cache that the stale content is still fresh should be used for subsequent requests until the content's new freshness lifetime is reached again.
If the content has changed, then the content is served as if the request were not conditional to begin with.
Conditional requests offer two benefits. Firstly, when making such a request to the origin server, if the content from the origin matches the content in the cache, this can be determined easily and without the overhead of transferring the entire resource.
Secondly, a well designed origin server will be designed in such
a way that conditional requests will be significantly cheaper to
produce than a full response. For static files, typically all that is
involved is a call to stat()
or similar system call, to
see if the file has changed in size or modification time. As such, even
local content may still be served faster from the cache if it has not
changed.
Origin servers should make every effort to support conditional requests as is practical, however if conditional requests are not supported, the origin will respond as if the request was not conditional, and the cache will respond as if the content had changed and save the new content to the cache. In this case, the cache will behave like a simple two state cache, where content is effectively either fresh or deleted.
The full definition of which responses can be cached by an HTTP cache is defined in RFC2616 Section 13.4 Response Cacheability, and can be summed up as follows:
CacheEnable
and CacheDisable
directives.CacheIgnoreNoLastMod
directive has been used to require otherwise.CacheStorePrivate
has been
used to require otherwise.CacheStoreNoStore
has been
used.It should be up to the client creating the request, or the origin
server constructing the response to decide whether or not the content
should be cacheable or not by correctly setting the
Cache-Control
header, and mod_cache
should
be left alone to honor the wishes of the client or server as appropriate.
Content that is time sensitive, or which varies depending on the
particulars of the request that are not covered by HTTP negotiation,
should not be cached. This content should declare itself uncacheable
using the Cache-Control
header.
If content changes often, expressed by a freshness lifetime of minutes or seconds, the content can still be cached, however it is highly desirable that the origin server supports conditional requests correctly to ensure that full responses do not have to be generated on a regular basis.
Content that varies based on client provided request headers can be
cached through intelligent use of the Vary
response
header.
When the origin server is designed to respond with different content based on the value of headers in the request, for example to serve multiple languages at the same URL, HTTP's caching mechanism makes it possible to cache multiple variants of the same page at the same URL.
This is done by the origin server adding a Vary
header
to indicate which headers must be taken into account by a cache when
determining whether two variants are different from one another.
If for example, a response is received