{"id":305243,"date":"2021-07-21T21:00:00","date_gmt":"2021-07-21T18:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-configure-cache-control-headers-in-apache-cloudsavvy-it\/"},"modified":"2021-07-21T21:00:00","modified_gmt":"2021-07-21T18:00:00","slug":"how-to-configure-cache-control-headers-in-apache-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-configure-cache-control-headers-in-apache-cloudsavvy-it\/","title":{"rendered":"#How to Configure Cache-Control Headers in Apache \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2650fd891f3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2650fd891f3\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-configure-cache-control-headers-in-apache-cloudsavvy-it\/#How_Does_Caching_Work\" >How Does Caching Work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-configure-cache-control-headers-in-apache-cloudsavvy-it\/#How_to_Use_Cache-Control_in_Apache\" >How to Use Cache-Control in Apache<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-configure-cache-control-headers-in-apache-cloudsavvy-it\/#Use_Surrogate-Control_to_Modify_CDN_Behavior_Directly\" >Use Surrogate-Control to Modify CDN Behavior Directly<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Configure Cache-Control Headers in Apache \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-1368\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/08\/028af1f2-1.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/08\/028af1f2-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/08\/028af1f2-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Each user\u2019s browser makes use of a built-in cache to store <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">download<\/a>ed objects, which can significantly speed up repeat visits to your website by loading from disk rather than the network. Here\u2019s how to configure it in Apache.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_Does_Caching_Work\"><\/span>How Does Caching Work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The first time a user connects to your site, they will download all static resources necessary to render the page, including things like your logo. When they navigate to a new page, it will load your logo from memory rather than asking for it again, speeding up performance significantly and reducing the load on your web server in the process.<\/p>\n<p>This is a client-side cache, but many sites will also make use of a Content Delivery Network, or CDN. A CDN is a network of servers that sit in front of your main web server, or \u201corigin\u201d server. This network caches your pages, increasing your maximum bandwidth, reducing the access latency, and greatly reducing the stress placed on your origin server. If you\u2019d like to learn more about CDNs, you can read our guide on them here.<\/p>\n<p><code>Cache-Control<\/code>\u00a0is a header that you can configure your web server to add to all outgoing requests, which will tell the browser and CDNs how to cache your content.<\/p>\n<p>Certain pages should\u00a0<em>never<\/em> be cached by shared caches like CDNs. Doing so will risk <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/arstechnica.com\/gaming\/2015\/12\/valve-explains-ddos-induced-caching-problem-led-to-xmas-day-steam-data-leaks-and-downtime\/\">displaying one user\u2019s personal information to others<\/a>.\u00a0As a <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> rule, if the page is going to be the exact same for all users, like your home page, you can cache it. If it shows confidential user info, you\u2019ll want to blacklist it from your cache. Static resources, like CSS and images, can usually be cached for everyone, often for much longer.<\/p>\n<p>The amount of time object spend in the cache is also important. Referred to as the Time-To-Live (TTL), the maximum age of your cached resources determines how long the object will stay in cache before being invalidated, and prompting the user to request a new object. For static resources that don\u2019t change much, you can set very high TTL values, usually around two years. For things that you might want to update, you\u2019ll want to set lower TTL values to prevent stale resources from being in the cache for too long.<\/p>\n<p>You can always use versioned filenames to trigger a cache reload. If you release a new version of a CSS style sheet, you can name it <code>styles-1.0.1.css<\/code>, and the user\u2019s browser (and any CDNs in front of it) will see it as a new file that needs to be redownloaded. Additionally, for some CDNs, you can issue manual invalidations to flush the existing cache without changing any filenames.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_to_Use_Cache-Control_in_Apache\"><\/span>How to Use Cache-Control in Apache<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>Cache-Control<\/code> has a few options:<\/p>\n<ul>\n<li><strong><code>public<\/code>\u00a0<\/strong>\u2013 May be cached by anyone, including browsers and CDNs. Use this for most static objects.<\/li>\n<li><strong><code>private<\/code>\u00a0<\/strong>\u2013 Contains sensitive data that cannot be cached by CDNs or reverse proxies. The user\u2019s browser may cache it locally. Use this for most authenticated pages.<\/li>\n<li><strong><code>no-cache<\/code><\/strong><strong>\u00a0<\/strong>\u2013 Despite the name, it doesn\u2019t disable caching. The browser may still cache the response for performance but must check with the origin server for updates before using it. Use this if you want the user to revalidate each time.<\/li>\n<li><code><strong>no-store<\/strong><\/code><strong>\u00a0<\/strong>\u2013\u00a0Disables caching entirely. Use this only for highly sensitive data that shouldn\u2019t be sent twice.<\/li>\n<\/ul>\n<p>Additionally, you can add the\u00a0<code>no-transform<\/code>\u00a0directive, which disables any conversions that may be done to the resource. For example, some CDNs compress images to reduce bandwidth. This directive disables that behavior.<\/p>\n<p>In Apache, you\u2019ll have to set this header manually using the\u00a0<code>Header set<\/code>\u00a0directive, like so:<\/p>\n<pre>Header set Cache-Control \"max-age=84600, public\"<\/pre>\n<p>The <code>max-age<\/code>\u00a0value is set in seconds, for example, <code>max-age=300<\/code> for a five-minute TTL, and <code>max-age=63072000<\/code> \u00a0for two years.<\/p>\n<p>You can put this directive in the root of your configuration to apply site-wide, but a better method is to apply the settings depending on the type of file. For example, to set a high TTL for most static <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>, you can use a <code>FilesMatch<\/code>\u00a0block:<\/p>\n<pre>&lt;FilesMatch \".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$\"&gt;&#13;\n  Header set Cache-Control \"max-age=63072000, public\"&#13;\n&lt;\/FilesMatch&gt;<\/pre>\n<p>If you want to blacklist a specific path from being cached by CDNs, you can use a <code>Directory<\/code> block:<\/p>\n<pre class=\"prettyprint lang-config prettyprinted\"><span class=\"pun\">&lt;<\/span><span class=\"tag\">Directory<\/span> <span class=\"str\">\"\/private\"<\/span><span class=\"pun\">&gt;&#13;\n Header set Cache-Control \"max-age=300, private\"&#13;\n&lt;\/<span class=\"tag\">Directory<\/span>&gt;<\/span>&#13;\n<\/pre>\n<p>Or simply match a single file:<\/p>\n<pre><span class=\"pun\">&lt;File\u00a0<\/span><span class=\"str\">\"protected.html\"<\/span><span class=\"pun\">&gt;&#13;\nHeader set Cache-Control \"max-age=300, private\"&#13;\n&lt;\/<span class=\"tag\">File<\/span>&gt;<\/span><\/pre>\n<p>The blocks with the more specific matches will take precedence over general regex matches, but you\u2019ll want to verify everything is being set properly on the receiving end. You can check this from Chrome\u2019s DevTools, under Network &gt; Headers.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignnone wp-image-3800 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/02\/e34f1f24.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"chrome devtools network tab\" width=\"700\" height=\"272\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you only have access to <code>.htaccess<\/code>\u00a0configuration, you can still use directory matching by creating a new <code>.htaccess<\/code>\u00a0file in each subdirectory.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Find Your Apache Configuration Folder<\/em><\/strong><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Use_Surrogate-Control_to_Modify_CDN_Behavior_Directly\"><\/span>Use Surrogate-Control to Modify CDN Behavior Directly<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The <code>Surrogate-Control<\/code>\u00a0header functions exactly like <code>Cache-Control<\/code>, but details specific instructions for CDNs and reverse proxies, rather than end users. This way, you can tell CDNs to do one thing, but send different directions to the browser.<\/p>\n<p>You\u2019ll have to set this header manually, in the same way as you set <code>Cache-Control<\/code>:<\/p>\n<pre><span class=\"pun\">Header set Surrogate-Control \"max-age=300, public\"<\/span><\/pre>\n<p>You will definitely want to test with your CDN to verify that this works\u2014<code>Surrogate-Control<\/code>\u00a0is fairly new, and isn\u2019t universal.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/3796\/how-to-configure-cache-control-headers-in-apache\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Configure Cache-Control Headers in Apache \u2013 CloudSavvy IT&#8221; Each user\u2019s browser makes use of a built-in cache to store downloaded objects, which can significantly speed up repeat visits to your website by loading from disk rather than the network. Here\u2019s how to configure it in Apache. How Does Caching Work? The first time&#8230;<\/p>\n","protected":false},"author":1,"featured_media":305244,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/08\/028af1f2-1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-305243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/305243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=305243"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/305243\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/305244"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=305243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=305243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=305243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}