{"id":1447,"date":"2018-10-23T12:18:47","date_gmt":"2018-10-23T12:18:47","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1447"},"modified":"2018-10-24T03:03:02","modified_gmt":"2018-10-24T03:03:02","slug":"nginx-browser-caching-linuxadmin-io","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/23\/nginx-browser-caching-linuxadmin-io\/","title":{"rendered":"Nginx Browser Caching &#8211; LinuxAdmin.io"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxadmin.io\/wp-content\/uploads\/2017\/06\/nginx_browser_caching-1.png\" alt=\"Nginx Browser Caching\" width=\"639\" height=\"237\" \/><\/p>\n<p>You can use Nginx to Set cache expiration times to leverage browser caching for the user requesting specific file types. This will cause the browser to retain the downloaded image until the length of the expires header. This will cause faster page time loads on each subsequent request performed by the end user.<\/p>\n<p>Prerequisites:<\/p>\n<p>You will need to have Nginx already installed. If you do not please see <a href=\"https:\/\/linuxadmin.io\/nginx-compile-source\/\">How To Install Nginx From Source On CentOS<\/a><\/p>\n<h2>How To Check Current Nginx Browser Cache Behavior<\/h2>\n<p>Check headers on a existing image:<\/p>\n<p>$ curl -Is http:\/\/domain.com\/test.png<\/p>\n<p>HTTP\/1.1 200 OK<br \/>\nServer: nginx\/1.11.13<br \/>\nDate: Thu, 08 Jun 2017 01:26:19 GMT<br \/>\nContent-Type: image\/png<br \/>\nContent-Length: 6983<br \/>\nLast-Modified: Fri, 28 Apr 2017 02:21:20 GMT<br \/>\nConnection: keep-alive<br \/>\nETag: &#8220;5902a720-1b47&#8221;<br \/>\nExpires: Thu, 15 Jun 2017 01:26:19 GMT<br \/>\nCache-Control: max-age=604800<br \/>\nAccept-Ranges: bytes<\/p>\n<p>If you look at the request, It is missing a Expires header. This is what needs to be in place to tell the browser to not check the file again until the cache time has expired.<\/p>\n<h2>Nginx Browser Cache Configuration<\/h2>\n<p>To implement this, you will need to edit your Nginx server configuration<\/p>\n<p>nano \/etc\/nginx\/nginx.conf<\/p>\n<p>Inside the server{} you will want to place the file types you would like to cache and their respective times:<\/p>\n<p>location ~* .(png|jpg|jpeg|gif|ico|otf)$ {<br \/>\nexpires 1y;<br \/>\n}<br \/>\nlocation ~* .(js|css)$ {<br \/>\nexpires 7d;<br \/>\n}<\/p>\n<p>The first block says for the following file types .png, .jpg, .jpeg, .gif, .ico, and .otf you want to cache them for a period of 1 year. The second location indicates you want to cache .js and .css files for a period of 7 days. You can add other file types or adjust the time as you see first based on the needs of your site. Once that has been completed, go ahead and save the file.<\/p>\n<p>Once that has been completed you will want restart Nginx to save the new configuration:<\/p>\n<p>service nginx restart<\/p>\n<p>You can then go ahead and check the same image as you did before adding the headers<\/p>\n<p>$ curl -Is http:\/\/domain.com\/test.png<\/p>\n<p>HTTP\/1.1 200 OK<br \/>\nDate: Thu, 08 Jun 2017 01:25:57 GMT<br \/>\nContent-Type: image\/png<br \/>\nContent-Length: 6983<br \/>\nConnection: keep-alive<br \/>\nLast-Modified: Fri, 28 Apr 2017 02:21:20 GMT<br \/>\nETag: &#8220;5902a720-1b47&#8221;<br \/>\nExpires: Fri, 25 May 2018 23:17:30 GMT<br \/>\nCache-Control: max-age=31536000<\/p>\n<p>You now see the Expires header and the time which should correspond with the length of the header you set on the file type. This will improve page speed for end users when they make multiple similar requests and it will also reduce overall server consumption per page request as well.<\/p>\n<p>Jun 7, 2017LinuxAdmin.io<\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/nginx-browser-caching\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use Nginx to Set cache expiration times to leverage browser caching for the user requesting specific file types. This will cause the browser to retain the downloaded image until the length of the expires header. This will cause faster page time loads on each subsequent request performed by the end user. Prerequisites: You &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/23\/nginx-browser-caching-linuxadmin-io\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Nginx Browser Caching &#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-1447","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\/1447","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=1447"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1447\/revisions"}],"predecessor-version":[{"id":1511,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1447\/revisions\/1511"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}