{"id":625,"date":"2018-10-17T23:40:58","date_gmt":"2018-10-17T23:40:58","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=625"},"modified":"2018-10-18T14:56:36","modified_gmt":"2018-10-18T14:56:36","slug":"mod_expires-configuration-in-apache-linuxadmin-io","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/mod_expires-configuration-in-apache-linuxadmin-io\/","title":{"rendered":"Mod_Expires Configuration In Apache &#8211; LinuxAdmin.io"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxadmin.io\/wp-content\/uploads\/2017\/10\/mod_expires-browser-caching.png\" alt=\"How to setup mod_expires in Apache\" width=\"667\" height=\"276\" \/><\/p>\n<p>mod_expires is a module which runs on the Apache web server. It allows manipulation of the cache control headers to leverage browser caching. What this means more specifically is that you will be able to set values on how long the image will be stored by the browser when a client makes a request. This will greatly improve page load times on subsequent requests by that same client. If a asset it set to not change very often, then using longer cache times are better, if the asset changes frequently you would want to use a shorter cache time so that returning visitors will see the updated asset. You can read more about the granular configuration on <a href=\"https:\/\/httpd.apache.org\/docs\/current\/mod\/mod_expires.html\">Apache\u2019s site<\/a>.<\/p>\n<p>This guide assumes you already have a working version of Apache web server. If you do not, please see <a href=\"https:\/\/linuxadmin.io\/compile-apache-2-4-source\/\">How To Install Apache<\/a>.<\/p>\n<h2>Verify mod_expires is loaded<\/h2>\n<p>First you will want to check to see if mod_expires is loaded by performing the following<\/p>\n<p>$ httpd -M 2&gt;&amp;1|grep expires<br \/>\nexpires_module (static)<\/p>\n<p>If it returns nothing, you will need to verify mod_expires is loaded the the config:<\/p>\n<p>cat httpd.conf|grep expires<br \/>\n#LoadModule expires_module modules\/mod_expires.so<\/p>\n<p>If it is commented out, you will want to uncomment it and restart Apache.<\/p>\n<h2>Configure mod_expires Rules<\/h2>\n<p>You will now want to set rules for your site. The configuration can either be placed in the .htaccess or directly in the Apache vhost stanza. The expiration time you will want to set largely depends on how long you plan on keeping the asset as it is. The below ruleset is fairly conservative, if you do not plan on updating those media types at all, you can set them for even a year before expiration.<\/p>\n<p>In this example we will just set the mod_expires values in the .htaccess file in the document root<\/p>\n<p>nano .htaccess<\/p>\n<p>Add the following, adjust any for longer or shorter times depending on your needs:<\/p>\n<p>&lt;IfModule mod_expires.c&gt;<br \/>\n# Turn on the module.<br \/>\nExpiresActive on<br \/>\n# Set the default expiry times.<br \/>\nExpiresDefault &#8220;access plus 2 days&#8221;<br \/>\nExpiresByType image\/jpg &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType image\/gif &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType image\/jpeg &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType image\/png &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType text\/css &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType text\/javascript &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType application\/javascript &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType application\/x-shockwave-flash &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType text\/css &#8220;now plus 1 month&#8221;<br \/>\nExpiresByType image\/ico &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType image\/x-icon &#8220;access plus 1 month&#8221;<br \/>\nExpiresByType text\/html &#8220;access plus 600 seconds&#8221;<br \/>\n&lt;\/IfModule&gt;<\/p>\n<p>Once you have set those values, further subsequent requests should now start setting expires headers. If you set the expires values directly in the Apache v-host stanza, you will want to restart Apache.<\/p>\n<h2>Testing mod_expires to ensure its working correctly<\/h2>\n<p>There are a couple different ways, you can use the developer tools in a browser to verify the expires value is being set correctly. You can also test this functionality with curl. You will want to curl the URL of the file you are checking<\/p>\n<p>$ curl -Is https:\/\/linuxadmin.io\/wp-content\/uploads\/2017\/04\/linuxadmin_io_logo.png<br \/>\nHTTP\/1.1 200 OK<br \/>\nDate: Mon, 09 Oct 2017 23:10:29 GMT<br \/>\nContent-Type: image\/png<br \/>\nContent-Length: 6983<br \/>\nConnection: keep-alive<br \/>\nSet-Cookie: __cfduid=d7768a9a20888ada8e0cee831245051cc1507590629; expires=Tue, 09-Oct-18 23:10:29 GMT; path=\/; domain=.linuxadmin.io; HttpOnly<br \/>\nLast-Modified: Fri, 28 Apr 2017 02:21:20 GMT<br \/>\nETag: &#8220;5902a720-1b47&#8221;<br \/>\nExpires: Sun, 30 Sep 2018 21:49:53 GMT<br \/>\nCache-Control: max-age=31536000<br \/>\nCF-Cache-Status: HIT<br \/>\nAccept-Ranges: bytes<br \/>\nServer: cloudflare-nginx<br \/>\nCF-RAY: 3ab5037b7ac291dc-EWR<\/p>\n<p>The line you are checking for is what starts with Expires:<\/p>\n<p>Expires: Sun, 30 Sep 2018 21:49:53 GMT<\/p>\n<p>this should return a time based on the mod_expires value you set. That is it for setting mod_expires headers in Apache to leverage browser caching.<\/p>\n<p>Oct 9, 2017LinuxAdmin.io<\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/mod_expires-configuration-apache\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>mod_expires is a module which runs on the Apache web server. It allows manipulation of the cache control headers to leverage browser caching. What this means more specifically is that you will be able to set values on how long the image will be stored by the browser when a client makes a request. This &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/mod_expires-configuration-in-apache-linuxadmin-io\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Mod_Expires Configuration In Apache &#8211; LinuxAdmin.io&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-625","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=625"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/625\/revisions"}],"predecessor-version":[{"id":752,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/625\/revisions\/752"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}