{"id":2099,"date":"2016-04-30T21:20:41","date_gmt":"2016-04-30T20:20:41","guid":{"rendered":"https:\/\/denbeke.be\/blog\/?p=2099"},"modified":"2016-04-30T21:20:41","modified_gmt":"2016-04-30T20:20:41","slug":"http-caching-mechanisms-in-caddy-server-etag-cache-control","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/software\/http-caching-mechanisms-in-caddy-server-etag-cache-control\/","title":{"rendered":"HTTP caching mechanisms in Caddy"},"content":{"rendered":"<p>Providing the ability for clients to cache static file requests is very important. It prevents the unneeded transfer of (static) files from server to client. Caching doesn&#8217;t only speedup things for the client, but also decreases the server load.<\/p>\n<p>But which caching mechanisms does Caddy provide, and how can they be enabled?<\/p>\n<h3 id=\"etag\">ETag<\/h3>\n<p>The HTTP ETag or entity tag is a header which contains an assigned identifier to each resource. The identifier is specific for each version of the resources. That way the client can do conditional requests by means of knowing whether a resource has changed or not.<\/p>\n<p>What should we do to enable it in Caddy?<br \/>\nWell&#8230; you mustn&#8217;t do anything. ETag support is built-in and works out of the box (on static files) with Caddy! (It is included since version 0.8.3.) \u2764\ufe0fCaddy!<\/p>\n<h3 id=\"cache-control\">Cache-Control<\/h3>\n<p>Another caching mechanism is the <code>Cache-Control<\/code> header field. This header field contains a value (in seconds) specifying how long the browser may cache a result (or whether it may even cache the request).<\/p>\n<p>Caddy doesn&#8217;t have a directive for this, so I even started writing code for a middleware, which, after a few minutes of research, seemed completely useless, since we can easily add custom headers in the Caddyfile.<\/p>\n<p>The Cache-Control values are fairly easy to understand and to use. For my WordPress blog I use these:<\/p>\n<pre><code>header \/blog\/wp-content\/ Cache-Control \"max-age=2592000\"\r\nheader \/blog\/wp-includes\/js\/ Cache-Control \"max-age=2592000\"<\/code><\/pre>\n<p><code>Cache-Control<\/code> is the name of the header field. <code>max-age<\/code> is the time interval (in seconds) for which the browser may cache the result. Other values of the <code>Cache-Control<\/code> header field:<\/p>\n<ul>\n<li><code>public, max-age=2592000<\/code>: responses may be cached by the client&#8217;s browser and all intermediary caches. The <code>public<\/code> directive is implicit so you don&#8217;t need to specify it.<\/li>\n<li><code>private, max-age=2592000<\/code>: responses may be cached by the client\u2019s browser only<\/li>\n<li><code>no-store<\/code>: completely disable caching<\/li>\n<\/ul>\n<h3 id=\"expires-header\">Expires header<\/h3>\n<p>The <code>expires<\/code> header is outdated by the <code>Cache-Control<\/code> header. It contained the exact date on which the cache of a resource should expire. This value is thus set dynamically, which is something we can&#8217;t do with the <code>header<\/code> directive of Caddy.<br \/>\nBut&#8230; In most modern systems the <code>Cache-Control<\/code> header is used instead of the <code>expires<\/code> header. So you can stick to the two above caching mechanisms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Providing the ability for clients to cache static file requests is very important. It prevents the unneeded transfer of (static) files from server to client. Caching doesn&#8217;t only speedup things for the client, but also decreases the server load. But which caching mechanisms does Caddy provide, and how can they be enabled? ETag The HTTP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[227,161],"tags":[260,188,151,186],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>HTTP caching mechanisms in Caddy &ndash; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/software\/http-caching-mechanisms-in-caddy-server-etag-cache-control\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HTTP caching mechanisms in Caddy &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"Providing the ability for clients to cache static file requests is very important. It prevents the unneeded transfer of (static) files from server to client. Caching doesn&#8217;t only speedup things for the client, but also decreases the server load. But which caching mechanisms does Caddy provide, and how can they be enabled? ETag The HTTP [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/software\/http-caching-mechanisms-in-caddy-server-etag-cache-control\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-30T20:20:41+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/software\/http-caching-mechanisms-in-caddy-server-etag-cache-control\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/software\/http-caching-mechanisms-in-caddy-server-etag-cache-control\/\",\"name\":\"HTTP caching mechanisms in Caddy &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"datePublished\":\"2016-04-30T20:20:41+00:00\",\"dateModified\":\"2016-04-30T20:20:41+00:00\",\"author\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/software\/http-caching-mechanisms-in-caddy-server-etag-cache-control\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/015ba35e6ce4f5859e3888ca99807575?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2099"}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=2099"}],"version-history":[{"count":1,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2099\/revisions"}],"predecessor-version":[{"id":2100,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2099\/revisions\/2100"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=2099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=2099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=2099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}