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.
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-Encodingset togzip, 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-Encodingand 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
- Upload Both Versions: Upload two versions of your files to S3—one plain and one gzipped. The gzipped version should have a
.gzsuffix. - Set Metadata: For gzipped files in S3, ensure the
Content-Encodingheader is set togzip. Use theaws s3 cpcommand with the--content-encodingflag to achieve this.
Related reading
- CloudWatch does not aggregate across dimensions for your custom metrics
- Cloudwatch failedinvocation error no logs available
- Cloudwatch Log Alert - How to include error / exception / stack trace data in email notification
- CNAME to s3 bucket amazon
- Combining microkernel pattern with microservices?
- Commit Failure in Paxos
- Clustering Algorithm for Paper Boys
- Code a linear programming exercise by hand

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.