CloudFront
S3
gzip
web performance
CDN

CloudFront with S3 website as origin is not serving gzipped files

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Amazon CloudFront is a comprehensive content delivery network (CDN) that accelerates the distribution of both static and dynamic content. When integrated with Amazon S3 (Simple Storage Service), CloudFront can serve content more efficiently. However, a frequent issue arises when CloudFront with an S3 website as the origin does not serve gzip-compressed files as expected. This article delves into the technical reasons behind this issue and discusses potential solutions to ensure your website delivers gzipped content effectively.

Understanding the Problem

Gzipping compresses files to reduce bandwidth consumption and improve page load times, which is essential for enhancing user experience. Ideally, when a browser requests a gzipped version of a file via CloudFront, it should be served automatically if the client supports gzip compression. However, when CloudFront is used with an S3 website origin, it may not serve pre-gzipped files unless explicitly configured to do so.

Technical Explanation

When a request is made, CloudFront checks if the request supports gzip by looking for the Accept-Encoding header set to gzip . If present, CloudFront attempts to deliver a gzipped version of the file from the cache or the origin. The challenge arises with S3 website origins because S3 does not automatically detect and serve gzipped files—it serves files as they are stored.

Key Points to Consider

  • S3 Website Hosting: The S3 website hosting configuration doesn't inherently recognize Content-Encoding set to gzip , leading to CloudFront potentially caching the non-gzipped version.
  • CloudFront Caching Behavior: CloudFront caches the response based on a variety of factors including HTTP headers. If gzipped files are not properly configured, CloudFront may cache uncompressed versions.
  • Content Neglect by S3: S3, when used as a static website, doesn't recognize Accept-Encoding and doesn’t handle content negotiation like an application server might.

Solution and Workaround

To address the issue of CloudFront not serving gzipped files from an S3 website, follow these strategies:

Configuring CloudFront to Serve Pre-Gzipped Files

  1. Upload Both Versions: Upload two versions of your files to S3—one plain and one gzipped. The gzipped version should have a .gz suffix.
  2. Set Metadata: For gzipped files in S3, ensure the Content-Encoding header is set to gzip . Use the aws s3 cp command with the --content-encoding flag to achieve this.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.