JA EN

Serving Optimal Images with Content Negotiation - Accept Headers and CDN Integration

· About 9 min read

What Is Content Negotiation - Selecting Optimal Formats via HTTP

Content Negotiation is a standard HTTP protocol feature where client and server negotiate the optimal response format. For image delivery, the browser communicates supported image formats to the server, which responds with the most efficient format available.

How it works:

Advantages: Unlike HTML <picture> element branching, formats can be switched without changing URLs. Existing <img src="photo.jpg"> tags remain unchanged while serving WebP or AVIF to supported browsers, requiring no HTML modifications.

Browser Accept header examples:

Safari added WebP support in iOS 16 / macOS Ventura (2023), and AVIF support from Safari 16.4 onwards.

Server-Side Implementation - Nginx and Apache Configuration Examples

Here's how to implement content negotiation on web servers, with configuration examples for both Nginx and Apache.

Nginx configuration:

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

server {
location ~* ^(.+)\.(jpe?g|png)$ {
set $base $1;
set $ext $2;
add_header Vary Accept;
try_files $base$img_suffix.$ext $uri =404;
}
}

This configuration returns the .avif version if image/avif is in the Accept header, the .webp version if image/webp is present, and falls back to the original JPEG/PNG if the alternative file doesn't exist.

Apache configuration (.htaccess):

RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/avif
RewriteCond %{REQUEST_FILENAME}.avif -f
RewriteRule ^(.+)\.(jpe?g|png)$ $1.$2.avif [T=image/avif,L]

RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_FILENAME}.webp -f
RewriteRule ^(.+)\.(jpe?g|png)$ $1.$2.webp [T=image/webp,L]

Header append Vary Accept

Critical note: Always include the Vary: Accept header. Without it, CDNs or proxies may cache the AVIF version and serve it to browsers that only support WebP, causing display failures.

Content Negotiation at the CDN Level - CloudFront and Cloudflare Setup

When using a CDN, implementing content negotiation at the CDN level is most efficient. Performing format detection at edge servers minimizes requests to the origin.

CloudFront configuration:

CloudFront Functions implementation:

function handler(event) {
var request = event.request;
var accept = request.headers.accept ? request.headers.accept.value : '';
var uri = request.uri;
if (uri.match(/\.(jpe?g|png)$/i)) {
if (accept.includes('image/avif')) {
request.uri = uri + '.avif';
} else if (accept.includes('image/webp')) {
request.uri = uri + '.webp';
}
}
return request;
}

Cloudflare setup: Enabling Cloudflare's Polish feature performs automatic WebP conversion at the edge. Pro plan and above also offers AVIF conversion. Custom logic can also be implemented via Transform Rules.

Proper Vary Header Management - Preventing Cache Accidents

The Vary header is a critical element ensuring correct caching behavior for content negotiation. Misconfiguration can cause serious incidents where wrong formats are served from cache.

Role of the Vary header: Vary: Accept tells caches that "this response varies based on the Accept header value." CDNs and proxies use this information to maintain separate cache entries for each Accept header value.

Common accident patterns:

Accept header normalization: Since Accept header values differ slightly between browsers (presence of quality values, ordering differences), using them directly as cache keys creates separate caches for browsers supporting the same format. Normalizing Accept headers at the CDN edge to 3 values (avif, webp, default) maximizes cache hit rates.

Testing method: Use curl -H "Accept: image/webp" -I https://example.com/photo.jpg to verify response headers return Content-Type: image/webp and Vary: Accept. Make multiple requests with different Accept headers to confirm correct formats are returned.

Comparison with picture Element - Client-Side vs Server-Side Approaches

Image format switching can be done via HTML <picture> element (client-side) or content negotiation (server-side). Understanding each approach's characteristics enables appropriate selection.

picture element (client-side):

<picture>
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" alt="Photo">
</picture>

Comparison:

Recommended usage:

Troubleshooting and Monitoring - Production Operations

Content negotiation works transparently when configured correctly, but issues can arise from configuration errors or CDN behavior changes. Here are monitoring and troubleshooting techniques for production environments.

Common problems and solutions:

Monitoring implementation:

Fallback reliability: The most critical requirement of content negotiation is reliably falling back to original images (JPEG/PNG) when no supported format is available. Always test that fallback paths remain intact when adding new formats. As an edge case, verify correct responses to requests with empty Accept headers (some bots and proxies).

Related Articles

Image CDN Setup and Optimization - High-Speed Delivery with CloudFront and Cloudflare

Learn how image CDNs work and how to configure them. Covers high-speed image delivery, dynamic resizing, format conversion, and caching strategies using CloudFront and Cloudflare.

The Future of Image Formats - How JPEG XL and WebP2 Will Transform the Web

Compare the technical features and future potential of next-gen image formats including JPEG XL, WebP2, and AVIF. Detailed analysis of compression performance, browser support, and migration strategies.

WebP to AVIF Migration Decision - Cost-Benefit Analysis and Implementation Strategy

Decision framework for migrating from WebP to AVIF. Covers additional compression gains, migration costs, and phased implementation strategies with concrete data.

Image Delivery Architecture for Large-Scale Sites - Design Patterns and Implementation

Architecture patterns for image delivery at scale. Covers CDN design, origin configuration, dynamic transformation, and caching strategies for high-traffic sites.

Complete Guide to Image Caching Strategies - Cache-Control, ETag, and CDN Configuration

Learn how to accelerate web image delivery with Cache-Control, ETag, and CDN caching. Covers cache invalidation strategies and versioning implementation patterns with practical examples.

AVIF Adoption Guide - Browser Support, Fallback Strategies, and Implementation

A practical guide to adopting AVIF format. Covers browser compatibility, picture element fallbacks, optimal encoding settings, and build pipeline integration.

Related Terms