How to Serve WebP and AVIF Images on WordPress (Nginx Configuration)

Images are usually the heaviest part of a WordPress page, and modern formats cut that weight a lot. WebP and AVIF on WordPress can shrink image files well below the same image saved as JPEG or PNG, with no visible quality loss at sensible settings. The catch is that WordPress alone does not serve them well: you need the files generated, the right MIME types, and Nginx rules that hand browsers the format they support.

This guide covers the full setup on a self-managed Nginx server: generating WebP and AVIF, configuring Nginx to serve them, and checking the result. It assumes you have root access to the server and a WordPress site already running on Nginx.

WebP and AVIF on WordPress: What Changes

WebP is supported by all modern browsers and typically produces smaller files than JPEG at the same quality. AVIF goes further, often producing smaller files than WebP, but encoding is slower and a few older browsers lack support. The practical approach is to serve AVIF first, fall back to WebP, then fall back to the original JPEG or PNG for anything older.

WebP and AVIF are the WordPress image optimization formats that matter most today. WordPress core supports uploading WebP files, but it does not generate WebP or AVIF versions of your uploads automatically, and it does not negotiate formats per browser. That negotiation is Nginx’s job, which is why the server configuration matters more than any plugin setting.

Step 1: Generate WebP and AVIF Versions

You have two routes. A plugin can generate modern-format versions on upload and rewrite image URLs, which suits owners who want no server work. The manual route suits people who want control: generate the variants with a tool like cwebp or avifenc, or with a conversion plugin that writes files next to the originals.

Whichever route you pick, keep the original files. AVIF encoding can be slow on large images, and you need the originals as fallback for older browsers and for future re-encoding at different quality settings. Name variants predictably, such as photo.jpg, photo.jpg.webp, and photo.jpg.avif, so the Nginx rules below can find them.

If your site has years of old uploads, convert in batches and test a few pages before converting everything. A broken image path across hundreds of posts is worse than a slow image. For general image handling, see our guide on lazy-loading images in WordPress without breaking layout, since lazy loading pairs well with smaller formats.

Step 2: Add MIME Types to Nginx

This Nginx WebP AVIF configuration starts with MIME types: Nginx needs to know the WebP and AVIF MIME types before it can serve them correctly. Add these to your mime.types file or directly in the server block:

types {
    image/webp webp;
    image/avif avif;
}

Most recent Nginx builds already include WebP. AVIF is the one people forget, and without it Nginx serves AVIF files with the wrong content type and browsers may refuse to display them. Check your mime.types file first so you do not add duplicates.

Step 3: Serve the Best Format With try_files

You serve AVIF via Nginx by letting the server check the Accept header before choosing a file. The cleanest approach uses Nginx content negotiation through that header. Browsers that support AVIF send image/avif in their Accept header, and browsers that support WebP send image/webp. A map block turns that into a suffix Nginx can try:

map $http_accept $img_suffix {
    default "";
    "~*avif" ".avif";
    "~*webp" ".webp";
}

Then, in your location block for uploads, try the suffixed file before the original:

location /wp-content/uploads/ {
    try_files $uri$img_suffix $uri =404;
}

One subtlety: the map checks AVIF before WebP only if the AVIF rule comes first and matches, which it does here because the first matching regex wins in Nginx maps. Test with browsers that support only WebP to confirm they still get WebP and not a 404. If a variant file does not exist, try_files falls back to the original, so missing variants never break a page.

Step 4: Cache Headers for Image Variants

Format negotiation and caching can clash. A cache that stores the AVIF response and serves it to a browser that only supports WebP breaks images. Add a Vary header so caches keep variants separate:

location ~* .(jpg|jpeg|png|webp|avif)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
    add_header Vary Accept;
}

The Vary Accept header tells CDNs and browsers that the response depends on the Accept header. If you use a CDN, confirm it respects Vary headers; some CDNs need that behavior enabled explicitly. This pairs with server caching: our guide on configuring Nginx FastCGI cache for WordPress without a plugin covers the caching layer this sits alongside.

Step 5: Test the Result

Test in three places: a modern browser with AVIF support, a browser with WebP but not AVIF, and a curl request with each Accept header. Check the Content-Type header on each response and confirm the file sizes drop compared to the original.

curl -H "Accept: image/avif,image/webp" -I https://example.com/wp-content/uploads/photo.jpg
curl -H "Accept: image/webp" -I https://example.com/wp-content/uploads/photo.jpg

Also test a page in your browser’s developer tools: filter the network tab by images and check the format column. If every image still shows JPEG, your map or location block is not matching, and the usual cause is the location block order or another uploads rule taking priority.

Common Problems and Fixes

  • AVIF served with the wrong content type: add the AVIF MIME type as shown in step 2 and reload Nginx.
  • Old browsers get broken images: confirm try_files falls back to $uri, and confirm the original file still exists on disk.
  • CDN serves the wrong variant: add Vary Accept and purge the CDN cache after changing the rules.
  • Images look worse: raise the quality setting on conversion. AVIF at very low quality shows banding in skies and gradients; a modest quality bump costs little size.
  • Uploads folder rewrite conflicts: check that no other location block for uploads overrides yours. Nginx uses the first matching regex location, so order matters.

Bringing It Together

Modern image formats are one of the cheapest speed wins on a WordPress site, because they shrink the largest assets on most pages without changing the design. The setup takes an hour: generate variants, add MIME types, add the negotiation map, set cache headers, and test all three browser paths. Once it works, every new upload follows the same pipeline, and the speed gain compounds as your media library grows.

For the rest of the stack, our LEMP stack install guide for WordPress on a VPS covers the server this configuration runs on, OPcache settings for WordPress on PHP cover the PHP side of speed, and Cloudflare APO and page rules add a caching layer in front of it all.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.