]> granicus.if.org Git - apache/blob - docs/manual/caching.html.en
Update transformations.
[apache] / docs / manual / caching.html.en
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5               This file is generated from xml source: DO NOT EDIT
6         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7       -->
8 <title>Caching Guide - Apache HTTP Server</title>
9 <link href="./style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="./style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="./style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="./images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/directives.html">Directives</a> | <a href="./faq/">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p>
15 <p class="apache">Apache HTTP Server Version 2.5</p>
16 <img alt="" src="./images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="./images/left.gif" /></a></div>
18 <div id="path">
19 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="./">Version 2.5</a></div><div id="page-content"><div id="preamble"><h1>Caching Guide</h1>
20 <div class="toplang">
21 <p><span>Available Languages: </span><a href="./en/caching.html" title="English">&nbsp;en&nbsp;</a> |
22 <a href="./fr/caching.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
23 <a href="./tr/caching.html" hreflang="tr" rel="alternate" title="Türkçe">&nbsp;tr&nbsp;</a></p>
24 </div>
25
26     <p>This document supplements the <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code>,
27     <code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code>, <code class="module"><a href="./mod/mod_file_cache.html">mod_file_cache</a></code> and <a href="programs/htcacheclean.html">htcacheclean</a> reference documentation.
28     It describes how to use the Apache HTTP Server's caching features to accelerate web and
29     proxy serving, while avoiding common problems and misconfigurations.</p>
30   </div>
31 <div id="quickview"><ul id="toc"><li><img alt="" src="./images/down.gif" /> <a href="#introduction">Introduction</a></li>
32 <li><img alt="" src="./images/down.gif" /> <a href="#http-caching">Three-state RFC2616 HTTP caching</a></li>
33 <li><img alt="" src="./images/down.gif" /> <a href="#socache-caching">Two-state Key/Value Shared Object Caching</a></li>
34 <li><img alt="" src="./images/down.gif" /> <a href="#file-caching">Specialized File Caching</a></li>
35 <li><img alt="" src="./images/down.gif" /> <a href="#security">Security Considerations</a></li>
36 </ul></div>
37 <div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
38 <div class="section">
39 <h2><a name="introduction" id="introduction">Introduction</a></h2>
40     
41     
42     <p>The Apache HTTP server offers a range of caching features that
43     are designed to improve the performance of the server in various
44     ways.</p>
45
46     <dl>
47         <dt>Three-state RFC2616 HTTP caching</dt>
48         <dd>
49             <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code>
50             and its provider modules
51             <code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code>
52             provide intelligent, HTTP-aware caching. The content itself is stored
53             in the cache, and mod_cache aims to honor all of the various HTTP
54             headers and options that control the cacheability of content
55             as described in
56             <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html">Section
57             13 of RFC2616</a>.
58             <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code>
59             is aimed at both simple and complex caching configurations, where
60             you are dealing with proxied content, dynamic local content or
61             have a need to speed up access to local files on a potentially
62             slow disk.
63         </dd>
64
65         <dt>Two-state key/value shared object caching</dt>
66         <dd>
67             <code class="module"><a href="./mod/mod_socache.html">mod_socache</a></code>
68             and its provider modules provide a
69             server wide key/value based shared object cache. These modules
70             are designed to cache low level data such as SSL sessions and
71             authentication credentials. Backends allow the data to be stored
72             server wide in shared memory, or datacenter wide in a cache such
73             as memcache or distcache.
74         </dd>
75
76         <dt>Specialized file caching</dt>
77         <dd>
78             <code class="module"><a href="./mod/mod_file_cache.html">mod_file_cache</a></code>
79             offers the ability to pre-load
80             files into memory on server startup, and can improve access
81             times and save file handles on files that are accessed often,
82             as there is no need to go to disk on each request.
83         </dd>
84     </dl>
85
86     <p>To get the most from this document, you should be familiar with
87     the basics of HTTP, and have read the Users' Guides to
88     <a href="urlmapping.html">Mapping URLs to the Filesystem</a> and
89     <a href="content-negotiation.html">Content negotiation</a>.</p>
90
91   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
92 <div class="section">
93 <h2><a name="http-caching" id="http-caching">Three-state RFC2616 HTTP caching</a></h2>
94
95     
96
97     <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code></li><li><code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_cache.html#cacheenable">CacheEnable</a></code></li><li><code class="directive"><a href="./mod/mod_cache.html#cachedisable">CacheDisable</a></code></li><li><code class="directive"><a href="./mod/core.html#usecanonicalname">UseCanonicalName</a></code></li><li><code class="directive"><a href="./mod/mod_negotiation.html#cachenegotiateddocs">CacheNegotiatedDocs</a></code></li></ul></td></tr></table>
98
99     <p>The HTTP protocol contains built in support for an in-line caching
100     mechanism 
101     <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html">
102     described by section 13 of RFC2616</a>, and the
103     <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> module can be used to take advantage of
104     this.</p>
105
106     <p>Unlike a simple two state key/value cache where the content
107     disappears completely when no longer fresh, an HTTP cache includes
108     a mechanism to retain stale content, and to ask the origin server
109     whether this stale content has changed and if not, make it fresh
110     again.</p>
111
112     <p>An entry in an HTTP cache exists in one of three states:</p>
113
114     <dl>
115     <dt>Fresh</dt>
116     <dd>
117         If the content is new enough (younger than its <strong>freshness
118         lifetime</strong>), it is considered <strong>fresh</strong>. An
119         HTTP cache is free to serve fresh content without making any
120         calls to the origin server at all.
121     </dd>
122     <dt>Stale</dt>
123     <dd>
124         <p>If the content is too old (older than its <strong>freshness
125         lifetime</strong>), it is considered <strong>stale</strong>. An
126         HTTP cache should contact the origin server and check whether
127         the content is still fresh before serving stale content to a
128         client. The origin server will either respond with replacement
129         content if not still valid, or ideally, the origin server will
130         respond with a code to tell the cache the content is still
131         fresh, without the need to generate or send the content again.
132         The content becomes fresh again and the cycle continues.</p>
133
134         <p>The HTTP protocol does allow the cache to serve stale data
135         under certain circumstances, such as when an attempt to freshen
136         the data with an origin server has failed with a 5xx error, or
137         when another request is already in the process of freshening
138         the given entry. In these cases a <code>Warning</code> header
139         is added to the response.</p>
140     </dd>
141     <dt>Non Existent</dt>
142     <dd>
143         If the cache gets full, it reserves the option to delete content
144         from the cache to make space. Content can be deleted at any time,
145         and can be stale or fresh. The <a href="programs/htcacheclean.html">htcacheclean</a> tool can be
146         run on a once off basis, or deployed as a daemon to keep the size
147         of the cache within the given size, or the given number of inodes.
148         The tool attempts to delete stale content before attempting to
149         delete fresh content.
150     </dd>
151     </dl>
152
153     <p>Full details of how HTTP caching works can be found in
154     <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html">
155     Section 13 of RFC2616</a>.</p>
156
157     <h3>Interaction with the Server</h3>
158       
159
160       <p>The <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> module hooks into the server in two
161       possible places depending on the value of the
162       <code class="directive"><a href="./mod/mod_cache.html#cachequickhandler">CacheQuickHandler</a></code> directive:
163       </p>
164
165       <dl>
166         <dt>Quick handler phase</dt>
167         <dd>
168           <p>This phase happens very early on during the request processing,
169               just after the request has been parsed. If the content is
170               found within the cache, it is served immediately and almost
171               all request processing is bypassed.</p>
172
173               <p>In this scenario, the cache behaves as if it has been "bolted
174               on" to the front of the server.</p>
175               
176               <p>This mode offers the best performance, as the majority of
177               server processing is bypassed. This mode however also bypasses the
178               authentication and authorization phases of server processing, so
179               this mode should be chosen with care when this is important.</p>
180           </dd>
181           <dt>Normal handler phase</dt>
182           <dd>
183               <p>This phase happens late in the request processing, after all
184               the request phases have completed.</p>
185
186               <p>In this scenario, the cache behaves as if it has been "bolted
187               on" to the back of the server.</p>
188
189               <p>This mode offers the most flexibility, as the potential exists
190               for caching to occur at a precisely controlled point in the filter
191               chain, and cached content can be filtered or personalized before
192               being sent to the client.</p>
193           </dd>
194         </dl>
195
196         <p>If the URL is not found within the cache, <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code>
197         will add a <a href="filter.html">filter</a> to the filter stack in order
198         to record the response to the cache, and then stand down, allowing normal
199         request processing to continue. If the content is determined to be
200         cacheable, the content will be saved to the cache for future serving,
201         otherwise the content will be ignored.</p>
202
203         <p>If the content found within the cache is stale, the
204         <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> module converts the request into a
205         <strong>conditional request</strong>. If the origin server responds with
206         a normal response, the normal response is cached, replacing the content
207         already cached. If the origin server responds with a 304 Not Modified
208         response, the content is marked as fresh again, and the cached content
209         is served by the filter instead of saving it.</p>
210     
211
212     <h3>Improving Cache Hits</h3>
213       
214
215       <p>When a virtual host is known by one of many different server aliases,
216       ensuring that <code class="directive"><a href="./mod/core.html#usecanonicalname">UseCanonicalName</a></code> is
217       set to <code>On</code> can dramatically improve the ratio of cache hits.
218       This is because the hostname of the virtual-host serving the content is
219       used within the cache key. With the setting set to <code>On</code>
220       virtual-hosts with multiple server names or aliases will not produce
221       differently cached entities, and instead content will be cached as
222       per the canonical hostname.</p>
223
224     
225
226     <h3>Freshness Lifetime</h3>
227       
228
229       <p>Well formed content that is intended to be cached should declare an
230       explicit freshness lifetime with the <code>Cache-Control</code>
231       header's <code>max-age</code> or <code>s-maxage</code> fields, or
232       by including an <code>Expires</code> header.</p>
233       
234       <p>At the same time, the origin server defined freshness lifetime can
235       be overridden by a client when the client presents their own
236       <code>Cache-Control</code> header within the request. In this case,
237       the lowest freshness lifetime between request and response wins.</p>
238
239       <p>When this freshness lifetime is missing from the request or the
240       response, a default freshness lifetime is applied. The default
241       freshness lifetime for cached entities is one hour, however
242       this can be easily over-ridden by using the <code class="directive"><a href="./mod/mod_cache.html#cachedefaultexpire">CacheDefaultExpire</a></code> directive.</p>
243
244       <p>If a response does not include an <code>Expires</code> header but does
245       include a <code>Last-Modified</code> header, <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code>
246       can infer a freshness lifetime based on a heuristic, which can be
247       controlled through the use of the <code class="directive"><a href="./mod/mod_cache.html#cachelastmodifiedfactor">CacheLastModifiedFactor</a></code> directive.</p>
248
249       <p>For local content, or for remote content that does not define its own
250       <code>Expires</code> header, <code class="module"><a href="./mod/mod_expires.html">mod_expires</a></code> may be used to
251       fine-tune the freshness lifetime by adding <code>max-age</code> and
252       <code>Expires</code>.</p>
253
254       <p>The maximum freshness lifetime may also be controlled by using the
255       <code class="directive"><a href="./mod/mod_cache.html#cachemaxexpire">CacheMaxExpire</a></code>.</p>
256
257     
258
259     <h3>A Brief Guide to Conditional Requests</h3>
260       
261
262       <p>When content expires from the cache and becomes stale, rather than
263       pass on the original request, httpd will modify the request to make
264       it conditional instead.</p>
265
266       <p>When an <code>ETag</code> header exists in the original cached
267       response, <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> will add an
268       <code>If-None-Match</code> header to the request to the origin server.
269       When a <code>Last-Modified</code> header exists in the original
270       cached response, <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> will add an
271       <code>If-Modified-Since</code> header to the request to the origin
272       server. Performing either of these actions makes the request
273       <strong>conditional</strong>.</p>
274
275       <p>When a conditional request is received by an origin server, the
276       origin server should check whether the ETag or the Last-Modified
277       parameter has changed, as appropriate for the request. If not, the
278       origin should respond with a terse "304 Not Modified" response. This
279       signals to the cache that the stale content is still fresh should be
280       used for subsequent requests until the content's new freshness lifetime
281       is reached again.</p>
282
283       <p>If the content has changed, then the content is served as if the
284       request were not conditional to begin with.</p>
285
286       <p>Conditional requests offer two benefits. Firstly, when making such
287       a request to the origin server, if the content from the origin
288       matches the content in the cache, this can be determined easily and
289       without the overhead of transferring the entire resource.</p>
290
291       <p>Secondly, a well designed origin server will be designed in such
292       a way that conditional requests will be significantly cheaper to
293       produce than a full response. For static files, typically all that is
294       involved is a call to <code>stat()</code> or similar system call, to
295       see if the file has changed in size or modification time. As such, even
296       local content may still be served faster from the cache if it has not
297       changed.</p>
298       
299       <p>Origin servers should make every effort to support conditional
300       requests as is practical, however if conditional requests are not
301       supported, the origin will respond as if the request was not
302       conditional, and the cache will respond as if the content had changed
303       and save the new content to the cache. In this case, the cache will
304       behave like a simple two state cache, where content is effectively
305       either fresh or deleted.</p>
306     
307
308     <h3>What Can be Cached?</h3>
309       
310
311       <p>The full definition of which responses can be cached by an HTTP
312       cache is defined in
313       <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4">
314       RFC2616 Section 13.4 Response Cacheability</a>, and can be summed up as
315       follows:</p>
316
317       <ol>
318         <li>Caching must be enabled for this URL. See the <code class="directive"><a href="./mod/mod_cache.html#cacheenable">CacheEnable</a></code> and <code class="directive"><a href="./mod/mod_cache.html#cachedisable">CacheDisable</a></code> directives.</li>
319
320         <li>The response must have a HTTP status code of 200, 203, 300, 301 or
321         410.</li>
322
323         <li>The request must be a HTTP GET request.</li>
324
325         <li>If the response contains an "Authorization:" header, it must
326         also contain an "s-maxage", "must-revalidate" or "public" option
327         in the "Cache-Control:" header, or it won't be cached.</li>
328
329         <li>If the URL included a query string (e.g. from a HTML form GET
330         method) it will not be cached unless the response specifies an
331         explicit expiration by including an "Expires:" header or the max-age
332         or s-maxage directive of the "Cache-Control:" header, as per RFC2616
333         sections 13.9 and 13.2.1.</li>
334
335         <li>If the response has a status of 200 (OK), the response must
336         also include at least one of the "Etag", "Last-Modified" or
337         the "Expires" headers, or the max-age or s-maxage directive of
338         the "Cache-Control:" header, unless the
339         <code class="directive"><a href="./mod/mod_cache.html#cacheignorenolastmod">CacheIgnoreNoLastMod</a></code>
340         directive has been used to require otherwise.</li>
341
342         <li>If the response includes the "private" option in a "Cache-Control:"
343         header, it will not be stored unless the
344         <code class="directive"><a href="./mod/mod_cache.html#cachestoreprivate">CacheStorePrivate</a></code> has been
345         used to require otherwise.</li>
346
347         <li>Likewise, if the response includes the "no-store" option in a
348         "Cache-Control:" header, it will not be stored unless the
349         <code class="directive"><a href="./mod/mod_cache.html#cachestorenostore">CacheStoreNoStore</a></code> has been
350         used.</li>
351
352         <li>A response will not be stored if it includes a "Vary:" header
353         containing the match-all "*".</li>
354       </ol>
355     
356
357     <h3>What Should Not be Cached?</h3>
358       
359
360       <p>It should be up to the client creating the request, or the origin
361       server constructing the response to decide whether or not the content
362       should be cacheable or not by correctly setting the
363       <code>Cache-Control</code> header, and <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> should
364       be left alone to honor the wishes of the client or server as appropriate.
365       </p>
366
367       <p>Content that is time sensitive, or which varies depending on the
368       particulars of the request that are not covered by HTTP negotiation,
369       should not be cached. This content should declare itself uncacheable
370       using the <code>Cache-Control</code> header.</p>
371       
372       <p>If content changes often, expressed by a freshness lifetime of minutes
373       or seconds, the content can still be cached, however it is highly
374       desirable that the origin server supports
375       <strong>conditional requests</strong> correctly to ensure that
376       full responses do not have to be generated on a regular basis.</p>
377
378       <p>Content that varies based on client provided request headers can be
379       cached through intelligent use of the <code>Vary</code> response
380       header.</p>
381
382     
383
384     <h3>Variable/Negotiated Content</h3>
385       
386
387       <p>When the origin server is designed to respond with different content
388       based on the value of headers in the request, for example to serve
389       multiple languages at the same URL, HTTP's caching mechanism makes it
390       possible to cache multiple variants of the same page at the same URL.</p>
391       
392       <p>This is done by the origin server adding a <code>Vary</code> header
393       to indicate which headers must be taken into account by a cache when
394       determining whether two variants are different from one another.</p>
395
396       <p>If for example, a response is received with a vary header such as;</p>
397
398       <div class="example"><p><code>
399 Vary: negotiate,accept-language,accept-charset
400       </code></p></div>
401
402       <p><code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> will only serve the cached content to
403       requesters with accept-language and accept-charset headers
404       matching those of the original request.</p>
405       
406       <p>Multiple variants of the content can be cached side by side,
407       <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> uses the <code>Vary</code> header and the
408       corresponding values of the request headers listed by <code>Vary</code>
409       to decide on which of many variants to return to the client.</p>
410     
411
412     <h3><a name="disk" id="disk">Caching to Disk</a></h3>
413       
414
415       <p>The <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> module relies on specific backend store
416       implementations in order to manage the cache, and for caching to disk
417       <code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code> is provided to support this.</p>
418
419       <p>Typically the module will be configured as so;</p>
420
421       <div class="example"><p><code>
422 CacheRoot   /var/cache/apache/<br />
423 CacheEnable disk /<br />
424 CacheDirLevels 2<br />
425 CacheDirLength 1
426       </code></p></div>
427
428       <p>Importantly, as the cached files are locally stored, operating system
429       in-memory caching will typically be applied to their access also. So
430       although the files are stored on disk, if they are frequently accessed
431       it is likely the operating system will ensure that they are actually
432       served from memory.</p>
433
434     
435
436     <h3>Understanding the Cache-Store</h3>
437       
438
439       <p>To store items in the cache, <code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code> creates
440       a 22 character hash of the URL being requested. This hash incorporates
441       the hostname, protocol, port, path and any CGI arguments to the URL,
442       as well as elements defined by the Vary header to ensure that multiple
443       URLs do not collide with one another.</p>
444
445       <p>Each character may be any one of 64-different characters, which mean
446       that overall there are 64^22 possible hashes. For example, a URL might
447       be hashed to <code>xyTGxSMO2b68mBCykqkp1w</code>. This hash is used
448       as a prefix for the naming of the files specific to that URL within
449       the cache, however first it is split up into directories as per
450       the <code class="directive"><a href="./mod/mod_cache_disk.html#cachedirlevels">CacheDirLevels</a></code> and
451       <code class="directive"><a href="./mod/mod_cache_disk.html#cachedirlength">CacheDirLength</a></code>
452       directives.</p>
453
454       <p><code class="directive"><a href="./mod/mod_cache_disk.html#cachedirlevels">CacheDirLevels</a></code>
455       specifies how many levels of subdirectory there should be, and
456       <code class="directive"><a href="./mod/mod_cache_disk.html#cachedirlength">CacheDirLength</a></code>
457       specifies how many characters should be in each directory. With
458       the example settings given above, the hash would be turned into
459       a filename prefix as
460       <code>/var/cache/apache/x/y/TGxSMO2b68mBCykqkp1w</code>.</p>
461
462       <p>The overall aim of this technique is to reduce the number of
463       subdirectories or files that may be in a particular directory,
464       as most file-systems slow down as this number increases. With
465       setting of "1" for
466       <code class="directive"><a href="./mod/mod_cache_disk.html#cachedirlength">CacheDirLength</a></code>
467       there can at most be 64 subdirectories at any particular level.
468       With a setting of 2 there can be 64 * 64 subdirectories, and so on.
469       Unless you have a good reason not to, using a setting of "1"
470       for <code class="directive"><a href="./mod/mod_cache_disk.html#cachedirlength">CacheDirLength</a></code>
471       is recommended.</p>
472
473       <p>Setting
474       <code class="directive"><a href="./mod/mod_cache_disk.html#cachedirlevels">CacheDirLevels</a></code>
475       depends on how many files you anticipate to store in the cache.
476       With the setting of "2" used in the above example, a grand
477       total of 4096 subdirectories can ultimately be created. With
478       1 million files cached, this works out at roughly 245 cached
479       URLs per directory.</p>
480
481       <p>Each URL uses at least two files in the cache-store. Typically
482       there is a ".header" file, which includes meta-information about
483       the URL, such as when it is due to expire and a ".data" file
484       which is a verbatim copy of the content to be served.</p>
485
486       <p>In the case of a content negotiated via the "Vary" header, a
487       ".vary" directory will be created for the URL in question. This
488       directory will have multiple ".data" files corresponding to the
489       differently negotiated content.</p>
490     
491
492     <h3>Maintaining the Disk Cache</h3>
493       
494
495       <p>The <code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code> module makes no attempt to
496       regulate the amount of disk space used by the cache, although it
497       will gracefully stand down on any disk error and behave as if the
498       cache was never present.</p>
499
500       <p>Instead, provided with httpd is the <a href="programs/htcacheclean.html">htcacheclean</a> tool which allows you
501       to clean the cache periodically. Determining how frequently to run <a href="programs/htcacheclean.html">htcacheclean</a> and what target size to
502       use for the cache is somewhat complex and trial and error may be needed to
503       select optimal values.</p>
504
505       <p><a href="programs/htcacheclean.html">htcacheclean</a> has two modes of
506       operation. It can be run as persistent daemon, or periodically from
507       cron. <a href="programs/htcacheclean.html">htcacheclean</a> can take up to an hour
508       or more to process very large (tens of gigabytes) caches and if you are
509       running it from cron it is recommended that you determine how long a typical
510       run takes, to avoid running more than one instance at a time.</p>
511  
512       <p>It is also recommended that an appropriate "nice" level is chosen for
513       htcacheclean so that the tool does not cause excessive disk io while the
514       server is running.</p>
515
516       <p class="figure">
517       <img src="images/caching_fig1.gif" alt="" width="600" height="406" /><br />
518       <a id="figure1" name="figure1"><dfn>Figure 1</dfn></a>: Typical
519       cache growth / clean sequence.</p>
520
521       <p>Because <code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code> does not itself pay attention
522       to how much space is used you should ensure that
523       <a href="programs/htcacheclean.html">htcacheclean</a> is configured to
524       leave enough "grow room" following a clean.</p>
525     
526
527   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
528 <div class="section">
529 <h2><a name="socache-caching" id="socache-caching">Two-state Key/Value Shared Object Caching</a></h2>
530
531     
532
533     <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_authn_socache.html">mod_authn_socache</a></code></li><li><code class="module"><a href="./mod/mod_socache_dbm.html">mod_socache_dbm</a></code></li><li><code class="module"><a href="./mod/mod_socache_dc.html">mod_socache_dc</a></code></li><li><code class="module"><a href="./mod/mod_socache_memcache.html">mod_socache_memcache</a></code></li><li><code class="module"><a href="./mod/mod_socache_shmcb.html">mod_socache_shmcb</a></code></li><li><code class="module"><a href="./mod/mod_ssl.html">mod_ssl</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_authn_socache.html#authncachesocache">AuthnCacheSOCache</a></code></li><li><code class="directive"><a href="./mod/mod_ssl.html#sslsessioncache">SSLSessionCache</a></code></li><li><code class="directive"><a href="./mod/mod_ssl.html#sslstaplingcache">SSLStaplingCache</a></code></li></ul></td></tr></table>
534     
535     <p>The Apache HTTP server offers a low level shared object cache for
536     caching information such as SSL sessions, or authentication credentials,
537     within the <a href="socache.html">socache</a> interface.</p>
538
539     <p>Additional modules are provided for each implementation, offering the
540     following backends:</p>
541
542     <dl>
543     <dt><code class="module"><a href="./mod/mod_socache_dbm.html">mod_socache_dbm</a></code></dt>
544     <dd>DBM based shared object cache.</dd>
545     <dt><code class="module"><a href="./mod/mod_socache_dc.html">mod_socache_dc</a></code></dt>
546     <dd>Distcache based shared object cache.</dd>
547     <dt><code class="module"><a href="./mod/mod_socache_memcache.html">mod_socache_memcache</a></code></dt>
548     <dd>Memcache based shared object cache.</dd>
549     <dt><code class="module"><a href="./mod/mod_socache_shmcb.html">mod_socache_shmcb</a></code></dt>
550     <dd>Shared memory based shared object cache.</dd>
551     </dl>
552
553     <h3><a name="mod_authn_socache-caching" id="mod_authn_socache-caching">Caching Authentication Credentials</a></h3>
554       
555
556       <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_authn_socache.html">mod_authn_socache</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_authn_socache.html#authncachesocache">AuthnCacheSOCache</a></code></li></ul></td></tr></table>
557
558       <p>The <code class="module"><a href="./mod/mod_authn_socache.html">mod_authn_socache</a></code> module allows the result of
559       authentication to be cached, relieving load on authentication backends.</p>
560
561     
562
563     <h3><a name="mod_ssl-caching" id="mod_ssl-caching">Caching SSL Sessions</a></h3>
564       
565
566       <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_ssl.html">mod_ssl</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_ssl.html#sslsessioncache">SSLSessionCache</a></code></li><li><code class="directive"><a href="./mod/mod_ssl.html#sslstaplingcache">SSLStaplingCache</a></code></li></ul></td></tr></table>
567
568       <p>The <code class="module"><a href="./mod/mod_ssl.html">mod_ssl</a></code> module uses the <code>socache</code> interface
569       to provide a session cache and a stapling cache.</p>
570
571     
572
573   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
574 <div class="section">
575 <h2><a name="file-caching" id="file-caching">Specialized File Caching</a></h2>
576
577     
578
579     <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_file_cache.html">mod_file_cache</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_file_cache.html#cachefile">CacheFile</a></code></li><li><code class="directive"><a href="./mod/mod_file_cache.html#mmapfile">MMapFile</a></code></li></ul></td></tr></table>
580
581     <p>On platforms where a filesystem might be slow, or where file
582     handles are expensive, the option exists to pre-load files into
583     memory on startup.</p>
584
585     <p>On systems where opening files is slow, the option exists to
586     open the file on startup and cache the file handle. These
587     options can help on systems where access to static files is
588     slow.</p>
589
590     <h3><a name="filehandle" id="filehandle">File-Handle Caching</a></h3>
591       
592
593       <p>The act of opening a file can itself be a source of delay, particularly
594       on network filesystems. By maintaining a cache of open file descriptors
595       for commonly served files, httpd can avoid this delay. Currently httpd
596       provides one implementation of File-Handle Caching.</p>
597
598       <h4>CacheFile</h4>
599         
600
601         <p>The most basic form of caching present in httpd is the file-handle
602         caching provided by <code class="module"><a href="./mod/mod_file_cache.html">mod_file_cache</a></code>. Rather than caching
603         file-contents, this cache maintains a table of open file descriptors. Files
604         to be cached in this manner are specified in the configuration file using
605         the <code class="directive"><a href="./mod/mod_file_cache.html#cachefile">CacheFile</a></code>
606         directive.</p>
607
608         <p>The
609         <code class="directive"><a href="./mod/mod_file_cache.html#cachefile">CacheFile</a></code> directive
610         instructs httpd to open the file when it is started and to re-use
611         this file-handle for all subsequent access to this file.</p>
612
613         <div class="example"><p><code>
614         CacheFile /usr/local/apache2/htdocs/index.html
615         </code></p></div>
616
617         <p>If you intend to cache a large number of files in this manner, you
618         must ensure that your operating system's limit for the number of open
619         files is set appropriately.</p>
620
621         <p>Although using <code class="directive"><a href="./mod/mod_file_cache.html#cachefile">CacheFile</a></code>
622         does not cause the file-contents to be cached per-se, it does mean
623         that if the file changes while httpd is running these changes will
624         not be picked up. The file will be consistently served as it was
625         when httpd was started.</p>
626
627         <p>If the file is removed while httpd is running, it will continue
628         to maintain an open file descriptor and serve the file as it was when
629         httpd was started. This usually also means that although the file
630         will have been deleted, and not show up on the filesystem, extra free
631         space will not be recovered until httpd is stopped and the file
632         descriptor closed.</p>
633       
634
635     
636
637     <h3><a name="inmemory" id="inmemory">In-Memory Caching</a></h3>
638       
639
640       <p>Serving directly from system memory is universally the fastest method
641       of serving content. Reading files from a disk controller or, even worse,
642       from a remote network is orders of magnitude slower. Disk controllers
643       usually involve physical processes, and network access is limited by
644       your available bandwidth. Memory access on the other hand can take mere
645       nano-seconds.</p>
646
647       <p>System memory isn't cheap though, byte for byte it's by far the most
648       expensive type of storage and it's important to ensure that it is used
649       efficiently. By caching files in memory you decrease the amount of
650       memory available on the system. As we'll see, in the case of operating
651       system caching, this is not so much of an issue, but when using
652       httpd's own in-memory caching it is important to make sure that you
653       do not allocate too much memory to a cache. Otherwise the system
654       will be forced to swap out memory, which will likely degrade
655       performance.</p>
656
657       <h4>Operating System Caching</h4>
658         
659
660         <p>Almost all modern operating systems cache file-data in memory managed
661         directly by the kernel. This is a powerful feature, and for the most
662         part operating systems get it right. For example, on Linux, let's look at
663         the difference in the time it takes to read a file for the first time
664         and the second time;</p>
665
666         <div class="example"><pre>
667 colm@coroebus:~$ time cat testfile &gt; /dev/null
668 real    0m0.065s
669 user    0m0.000s
670 sys     0m0.001s
671 colm@coroebus:~$ time cat testfile &gt; /dev/null
672 real    0m0.003s
673 user    0m0.003s
674 sys     0m0.000s</pre></div>
675
676         <p>Even for this small file, there is a huge difference in the amount
677         of time it takes to read the file. This is because the kernel has cached
678         the file contents in memory.</p>
679
680         <p>By ensuring there is "spare" memory on your system, you can ensure
681         that more and more file-contents will be stored in this cache. This
682         can be a very efficient means of in-memory caching, and involves no
683         extra configuration of httpd at all.</p>
684
685         <p>Additionally, because the operating system knows when files are
686         deleted or modified, it can automatically remove file contents from the
687         cache when necessary. This is a big advantage over httpd's in-memory
688         caching which has no way of knowing when a file has changed.</p>
689       
690
691       <p>Despite the performance and advantages of automatic operating system
692       caching there are some circumstances in which in-memory caching may be
693       better performed by httpd.</p>
694
695       <h4>MMapFile Caching</h4>
696         
697
698         <p><code class="module"><a href="./mod/mod_file_cache.html">mod_file_cache</a></code> provides the
699         <code class="directive"><a href="./mod/mod_file_cache.html#mmapfile">MMapFile</a></code> directive, which
700         allows you to have httpd map a static file's contents into memory at
701         start time (using the mmap system call). httpd will use the in-memory
702         contents for all subsequent accesses to this file.</p>
703
704         <div class="example"><p><code>
705         MMapFile /usr/local/apache2/htdocs/index.html
706         </code></p></div>
707
708         <p>As with the
709         <code class="directive"><a href="./mod/mod_file_cache.html#cachefile">CacheFile</a></code> directive, any
710         changes in these files will not be picked up by httpd after it has
711         started.</p>
712
713         <p> The <code class="directive"><a href="./mod/mod_file_cache.html#mmapfile">MMapFile</a></code>
714         directive does not keep track of how much memory it allocates, so
715         you must ensure not to over-use the directive. Each httpd child
716         process will replicate this memory, so it is critically important
717         to ensure that the files mapped are not so large as to cause the
718         system to swap memory.</p>
719       
720     
721
722   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
723 <div class="section">
724 <h2><a name="security" id="security">Security Considerations</a></h2>
725     
726
727     <h3>Authorization and Access Control</h3>
728       
729
730       <p>Using <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> in its default state where
731       <code class="directive"><a href="./mod/mod_cache.html#cachequickhandler">CacheQuickHandler</a></code> is set to
732       <code>On</code> is very much like having a caching reverse-proxy bolted
733       to the front of the server. Requests will be served by the caching module
734       unless it determines that the origin server should be queried just as an
735       external cache would, and this drastically changes the security model of
736       httpd.</p>
737
738       <p>As traversing a filesystem hierarchy to examine potential
739       <code>.htaccess</code> files would be a very expensive operation,
740       partially defeating the point of caching (to speed up requests),
741       <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> makes no decision about whether a cached
742       entity is authorised for serving. In other words; if
743       <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> has cached some content, it will be served
744       from the cache as long as that content has not expired.</p>
745
746       <p>If, for example, your configuration permits access to a resource by IP
747       address you should ensure that this content is not cached. You can do this
748       by using the <code class="directive"><a href="./mod/mod_cache.html#cachedisable">CacheDisable</a></code>
749       directive, or <code class="module"><a href="./mod/mod_expires.html">mod_expires</a></code>. Left unchecked,
750       <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> - very much like a reverse proxy - would cache
751       the content when served and then serve it to any client, on any IP
752       address.</p>
753
754       <p>When the <code class="directive"><a href="./mod/mod_cache.html#cachequickhandler">CacheQuickHandler</a></code>
755       directive is set to <code>Off</code>, the full set of request processing
756       phases are executed and the security model remains unchanged.</p>
757     
758
759     <h3>Local exploits</h3>
760       
761
762       <p>As requests to end-users can be served from the cache, the cache
763       itself can become a target for those wishing to deface or interfere with
764       content. It is important to bear in mind that the cache must at all
765       times be writable by the user which httpd is running as. This is in
766       stark contrast to the usually recommended situation of maintaining
767       all content unwritable by the Apache user.</p>
768
769       <p>If the Apache user is compromised, for example through a flaw in
770       a CGI process, it is possible that the cache may be targeted. When
771       using <code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code>, it is relatively easy to
772       insert or modify a cached entity.</p>
773
774       <p>This presents a somewhat elevated risk in comparison to the other
775       types of attack it is possible to make as the Apache user. If you are
776       using <code class="module"><a href="./mod/mod_cache_disk.html">mod_cache_disk</a></code> you should bear this in mind -
777       ensure you upgrade httpd when security upgrades are announced and
778       run CGI processes as a non-Apache user using <a href="suexec.html">suEXEC</a> if possible.</p>
779
780     
781
782     <h3>Cache Poisoning</h3>
783       
784
785       <p>When running httpd as a caching proxy server, there is also the
786       potential for so-called cache poisoning. Cache Poisoning is a broad
787       term for attacks in which an attacker causes the proxy server to
788       retrieve incorrect (and usually undesirable) content from the origin
789       server.</p>
790
791       <p>For example if the DNS servers used by your system running httpd
792       are vulnerable to DNS cache poisoning, an attacker may be able to control
793       where httpd connects to when requesting content from the origin server.
794       Another example is so-called HTTP request-smuggling attacks.</p>
795
796       <p>This document is not the correct place for an in-depth discussion
797       of HTTP request smuggling (instead, try your favourite search engine)
798       however it is important to be aware that it is possible to make
799       a series of requests, and to exploit a vulnerability on an origin
800       webserver such that the attacker can entirely control the content
801       retrieved by the proxy.</p>
802     
803
804     <h3>Denial of Service / Cachebusting</h3>
805       
806
807       <p>The Vary mechanism allows multiple variants of the same URL to be
808       cached side by side. Depending on header values provided by the client,
809       the cache will select the correct variant to return to the client. This
810       mechanism can become a problem when an attempt is made to vary on a
811       header that is known to contain a wide range of possible values under
812       normal use, for example the <code>User-Agent</code> header. Depending
813       on the popularity of the particular web site thousands or millions of
814       duplicate cache entries could be created for the same URL, crowding
815       out other entries in the cache.</p>
816       
817       <p>In other cases, there may be a need to change the URL of a particular
818       resource on every request, usually by adding a "cachebuster" string to
819       the URL. If this content is declared cacheable by a server for a
820       significant freshness lifetime, these entries can crowd out
821       legitimate entries in a cache. While <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code>
822       provides a
823       <code class="directive"><a href="./mod/mod_cache.html#cacheignoreurlsessionidentifiers">CacheIgnoreURLSessionIdentifiers</a></code>
824       directive, this directive should be used with care to ensure that
825       downstream proxy or browser caches aren't subjected to the same denial
826       of service issue.</p>
827     
828   </div></div>
829 <div class="bottomlang">
830 <p><span>Available Languages: </span><a href="./en/caching.html" title="English">&nbsp;en&nbsp;</a> |
831 <a href="./fr/caching.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
832 <a href="./tr/caching.html" hreflang="tr" rel="alternate" title="Türkçe">&nbsp;tr&nbsp;</a></p>
833 </div><div id="footer">
834 <p class="apache">Copyright 2011 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
835 <p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/directives.html">Directives</a> | <a href="./faq/">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p></div>
836 </body></html>