caching.xml revision 02df76a3dae0c5bf05c9329eccaad449a66bfc68
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose<!DOCTYPE manualpage SYSTEM "style/manualpage.dtd">
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose<?xml-stylesheet type="text/xsl" href="style/manual.en.xsl"?>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose<!-- $LastChangedRevision$ -->
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose Licensed to the Apache Software Foundation (ASF) under one or more
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose contributor license agreements. See the NOTICE file distributed with
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose this work for additional information regarding copyright ownership.
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose The ASF licenses this file to You under the Apache License, Version 2.0
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose (the "License"); you may not use this file except in compliance with
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose the License. You may obtain a copy of the License at
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose Unless required by applicable law or agreed to in writing, software
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose distributed under the License is distributed on an "AS IS" BASIS,
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose See the License for the specific language governing permissions and
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose limitations under the License.
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>This document supplements the <module>mod_cache</module>,
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <module>mod_disk_cache</module>, <module>mod_mem_cache</module>,
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose href="programs/htcacheclean.html">htcacheclean</a> reference documentation.
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose It describes how to use Apache's caching features to accelerate web and
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose proxy serving, while avoiding common problems and misconfigurations.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>As of Apache HTTP server version 2.2 <module>mod_cache</module>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose and <module>mod_file_cache</module> are no longer marked
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose experimental and are considered suitable for production use. These
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose caching architectures provide a powerful means to accelerate HTTP
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose handling, both as an origin webserver and as a proxy.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p><module>mod_cache</module> and its provider modules
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <module>mod_mem_cache</module> and <module>mod_disk_cache</module>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose provide intelligent, HTTP-aware caching. The content itself is stored
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose in the cache, and mod_cache aims to honour all of the various HTTP
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose headers and options that control the cachability of content. It can
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose handle both local and proxied content. <module>mod_cache</module>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose is aimed at both simple and complex caching configurations, where
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose you are dealing with proxied content, dynamic local content or
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose have a need to speed up access to local files which change with
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p><module>mod_file_cache</module> on the other hand presents a more
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose basic, but sometimes useful, form of caching. Rather than maintain
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose the complexity of actively ensuring the cachability of URLs,
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <module>mod_file_cache</module> offers file-handle and memory-mapping
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose tricks to keep a cache of files as they were when Apache was last
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose started. As such, <module>mod_file_cache</module> is aimed at improving
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose the access time to local static files which do not change very
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>As <module>mod_file_cache</module> presents a relatively simple
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose caching implementation, apart from the specific sections on <directive
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose module="mod_file_cache">CacheFile</directive> and <directive
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose module="mod_file_cache">MMapStatic</directive>, the explanations
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose in this guide cover the <module>mod_cache</module> caching
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose architecture.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>To get the most from this document, you should be familiar with
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose the basics of HTTP, and have read the Users' Guides to
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <a href="urlmapping.html">Mapping URLs to the Filesystem</a> and
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <a href="content-negotiation.html">Content negotiation</a>.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <modulelist>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose </modulelist>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directivelist>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="mod_cache">CacheEnable</directive>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="mod_cache">CacheDisable</directive>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="mod_file_cache">MMapStatic</directive>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="mod_file_cache">CacheFile</directive>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="mod_file_cache">CacheFile</directive>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="core">UseCanonicalName</directive>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="mod_negotiation">CacheNegotiatedDocs</directive>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose </directivelist>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>There are two main stages in <module>mod_cache</module> that can
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose occur in the lifetime of a request. First, <module>mod_cache</module>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose is a URL mapping module, which means that if a URL has been cached,
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose and the cached version of that URL has not expired, the request will
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose be served directly by <module>mod_cache</module>.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>This means that any other stages that might ordinarily happen
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose in the process of serving a request -- for example being handled
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose by <module>mod_proxy</module>, or <module>mod_rewrite</module> --
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose won't happen. But then this is the point of caching content in
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose the first place.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>If the URL is not found within the cache, <module>mod_cache</module>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose will add a <a href="filter.html">filter</a> to the request handling. After
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose Apache has located the content by the usual means, the filter will be run
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose as the content is served. If the content is determined to be cacheable,
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose the content will be saved to the cache for future serving.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>If the URL is found within the cache, but also found to have expired,
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose the filter is added anyway, but <module>mod_cache</module> will create
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose a conditional request to the backend, to determine if the cached version
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose is still current. If the cached version is still current, its
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose meta-information will be updated and the request will be served from the
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose cache. If the cached version is no longer current, the cached version
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose will be deleted and the filter will save the updated content to the cache
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose as it is served.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>When caching locally generated content, ensuring that
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="core">UseCanonicalName</directive> is set to
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <code>On</code> can dramatically improve the ratio of cache hits. This
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose is because the hostname of the virtual-host serving the content forms
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose a part of the cache key. With the setting set to <code>On</code>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose virtual-hosts with multiple server names or aliases will not produce
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose differently cached entities, and instead content will be cached as
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose per the canonical hostname.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>Because caching is performed within the URL to filename translation
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose phase, cached documents will only be served in response to URL requests.
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose Ordinarily this is of little consequence, but there is one circumstance
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose in which it matters: If you are using <a href="howto/ssi.html">Server
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose<!-- The following include can be cached -->
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose<!--#include virtual="/footer.html" -->
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose<!-- The following include can not be cached -->
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose<!--#include file="/path/to/footer.html" --></pre>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>If you are using Server Side Includes, and want the benefit of speedy
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose serves from the cache, you should use <code>virtual</code> include
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>The default expiry period for cached entities is one hour, however
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose this can be easily over-ridden by using the <directive
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose module="mod_cache">CacheDefaultExpire</directive> directive. This
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose default is only used when the original source of the content does not
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose specify an expire time or time of last modification.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>If a response does not include an <code>Expires</code> header but does
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose include a <code>Last-Modified</code> header, <module>mod_cache</module>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose can infer an expiry period based on the use of the <directive
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose module="mod_cache">CacheLastModifiedFactor</directive> directive.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>For local content, <module>mod_expires</module> may be used to
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose fine-tune the expiry period.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>The maximum expiry period may also be controlled by using the
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <directive module="mod_cache">CacheMaxExpire</directive>.</p>
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek <title>A Brief Guide to Conditional Requests</title>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>When content expires from the cache and is re-requested from the
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose backend or content provider, rather than pass on the original request,
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose Apache will use a conditional request instead.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>HTTP offers a number of headers which allow a client, or cache
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose to discern between different versions of the same content. For
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose example if a resource was served with an "Etag:" header, it is
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose possible to make a conditional request with an "If-Match:"
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek header. If a resource was served with a "Last-Modified:" header
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose it is possible to make a conditional request with an
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose "If-Modified-Since:" header, and so on.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>When such a conditional request is made, the response differs
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose depending on whether the content matches the conditions. If a request is
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose made with an "If-Modified-Since:" header, and the content has not been
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose modified since the time indicated in the request then a terse "304 Not
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose Modified" response is issued.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>If the content has changed, then it is served as if the request were
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose not conditional to begin with.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>The benefits of conditional requests in relation to caching are
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose twofold. Firstly, when making such a request to the backend, if the
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose content from the backend matches the content in the store, this can be
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose determined easily and without the overhead of transferring the entire
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose resource.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p>Secondly, conditional requests are usually less strenuous on the
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose backend. For static files, typically all that is involved is a call
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose to <code>stat()</code> or similar system call, to see if the file has
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose changed in size or modification time. As such, even if Apache is
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose caching local content, even expired content may still be served faster
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose from the cache if it has not changed. As long as reading from the cache
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose store is faster than reading from the backend (e.g. an in-memory cache
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose compared to reading from disk).</p>
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek <p>As mentioned already, the two styles of caching in Apache work
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek differently, <module>mod_file_cache</module> caching maintains file
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek contents as they were when Apache was started. When a request is
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek made for a file that is cached by this module, it is intercepted
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose and the cached file is served.</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <p><module>mod_cache</module> caching on the other hand is more
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose complex. When serving a request, if it has not been cached
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose previously, the caching module will determine if the content
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose is cacheable. The conditions for determining cachability of
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose a response are;</p>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <li>Caching must be enabled for this URL. See the <directive
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose module="mod_cache">CacheEnable</directive> and <directive
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose module="mod_cache">CacheDisable</directive> directives.</li>
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <li>The response must have a HTTP status code of 200, 203, 300, 301 or
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose <li>If the request contains an "Authorization:" header, the response
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose will not be cached.</li>
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek <li>If the response contains an "Authorization:" header, it must
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose also contain an "s-maxage", "must-revalidate" or "public" option