{"id":5794,"date":"2021-04-28T14:14:18","date_gmt":"2021-04-28T14:14:18","guid":{"rendered":"https:\/\/shortpixel.com/blog\/?p=5794"},"modified":"2021-08-06T10:25:46","modified_gmt":"2021-08-06T10:25:46","slug":"avif-mime-type-delivery-apache-nginx","status":"publish","type":"post","link":"https:\/\/shortpixel.com\/blog\/avif-mime-type-delivery-apache-nginx\/","title":{"rendered":"How to configure your web server to deliver AVIF images"},"content":{"rendered":"\n<p>ShortPixel Image Optimizer is able to <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/shortpixel.com/blog\/shortpixel-image-optimizer-the-only-wp-plugin-that-creates-avif-images\/\" target=\"_blank\">create and serve AVIF files<\/a> since version 4.22. The delivery can be done in two different ways: using the &lt;PICTURE&gt; tag syntax or without altering the page code (via .htaccess).<\/p>\n\n\n\n<p>The recommended method is via .htaccess, but it&#8217;s only available if you are using Apache as web server. If you are using NGINX, and if you are having trouble after creating AVIF files on your site and the <a rel=\"noreferrer noopener\" aria-label=\"supporting browsers (opens in a new tab)\" href=\"https:\/\/caniuse.com\/avif\" target=\"_blank\">supporting browsers<\/a> are downloading the images instead of displaying them, you have come to the the right place. <\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Apache<\/h2>\n\n\n\n<p>Apache does <strong>not<\/strong> include by default the <code>image\/avif<\/code> or <code>image\/avif-sequence<\/code> MIME types in the standard configuration files. As a result, on a standard Apache configuration, the AVIF files <strong>might<\/strong> be delivered when an HTML page is requested. But when visited directly, most probably they won&#8217;t work and in some cases the browser will try to download and save the file because they might inherit the default <code>content-type<\/code> header, which in most cases is set to <code>application\/octet-stream<\/code>. And in order to have the AVIF images displayed by the browsers, they should have the <code>content-type<\/code> header set to <code>image\/avif<\/code>. <\/p>\n\n\n\n<p>The solution to this is very simple, and you need to add these 4 lines to the site&#8217;s .htaccess file (which is usually located in the root folder of the site):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_mime.c&gt;\n    AddType image\/avif                                  avif\n    AddType image\/avif-sequence                         avifs\n&lt;\/IfModule&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2021\/08\/AVIF-MIME-type-Apache.jpg\"><img decoding=\"async\" width=\"907\" height=\"174\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2021\/08\/AVIF-MIME-type-Apache.jpg\" alt=\"AVIF MIME-type Apache\" class=\"wp-image-5891\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2021\/08\/AVIF-MIME-type-Apache.jpg 907w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2021\/08\/AVIF-MIME-type-Apache-300x58.jpg 300w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2021\/08\/AVIF-MIME-type-Apache-768x147.jpg 768w\" sizes=\"(max-width: 907px) 100vw, 907px\" \/><\/a><figcaption>Set AVIF MIME type in Apache<\/figcaption><\/figure>\n\n\n\n<p>After saving the .htaccess file, the AVIF files should have the correct Content-type header and the browsers that support this image format should be able to open them with no more issues!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">NGINX<\/h2>\n\n\n\n<p>NGINX does <strong>not<\/strong> include by default the <code>image\/avif<\/code> or <code>image\/avif-sequence<\/code> MIME types in their default configuration files, just like Apache. There was <a rel=\"noreferrer noopener\" aria-label=\"an attempt (opens in a new tab)\" href=\"https:\/\/forum.nginx.org\/read.php?29,286898,286898#msg-286898\" target=\"_blank\">an attempt<\/a> to add a patch for this, but it looks like it was rejected. The result is that on a standard NGINX configuration, the AVIF files will be delivered when an HTML page is requested, but when visited directly, they will inherit the default <code>content-type<\/code> header, which in most cases is set to <code>application\/octet-stream<\/code>, and that causes the file to be downloaded. Here as well, the correct <code>content-type<\/code> header needs to be set to <code>image\/avif<\/code>.<\/p>\n\n\n\n<p>The solution to this is also quite simple, as long as you have access to the NGINX configuration files. You basically need to look for the file named <strong>mime.types<\/strong>, which is usually located in the root of the NGINX installation (\/etc\/nginx\/), and add the two MIME types: <code>image\/avif<\/code> and <code>image\/avif-sequence<\/code>. You can add them just like in this image, preferably under the <code>image\/webp<\/code> MIME Type:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"374\" height=\"191\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2021\/04\/AVIF_MIME_Type_NGINX.jpg\" alt=\"\" class=\"wp-image-5795\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2021\/04\/AVIF_MIME_Type_NGINX.jpg 374w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2021\/04\/AVIF_MIME_Type_NGINX-300x153.jpg 300w\" sizes=\"(max-width: 374px) 100vw, 374px\" \/><figcaption>How to set AVIF MIME Type in NGINX<\/figcaption><\/figure><\/div>\n\n\n\n<p>After saving the mime.types config file, all you have to do is <strong>reload NGINX<\/strong>, by typing:<\/p>\n\n\n\n<p><code>service nginx reload<\/code><\/p>\n\n\n\n<p>After NGINX has reloaded, the images should be properly delivered to the supporting browsers, according to the delivery method of your choice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ShortPixel Image Optimizer is able to create and serve AVIF files since version 4.22. The delivery can be done in two different ways: using the &lt;PICTURE&gt; tag syntax or without altering the page code (via .htaccess). The recommended method is via .htaccess, but it&#8217;s only available if you are using Apache as web server. If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5798,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-5794","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-image-optimization"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/comments?post=5794"}],"version-history":[{"count":7,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5794\/revisions"}],"predecessor-version":[{"id":5892,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5794\/revisions\/5892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media\/5798"}],"wp:attachment":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media?parent=5794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/categories?post=5794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/tags?post=5794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}