Sub topic 10
Default root object and custom error pages
This chapter assumes you have a working distribution with a private S3 bucket behind Origin Access Control, and that you know basic HTTP status codes. It covers two settings that decide what viewers see for the site root and for missing or forbidden files.
The default root object
A request for https://d111111abcdef8.cloudfront.net/ has no object key. S3 has no concept of an index file at its REST endpoint, so CloudFront needs to know which key to ask for. The default root object supplies it. When you set it to index.html, CloudFront rewrites a request for / to /index.html before it goes to the origin.
Three points matter:
- The setting applies only to the root URL of the distribution. A request for
/docs/is not rewritten to/docs/index.html. - The value is an object key with no leading slash. Write
index.html, not/index.html. - Without it, the request for
/goes to the bucket as a request for the bucket itself. You get an error or a list of keys instead of your page.
In the CloudFormation excerpt from the distribution chapter, this is the DefaultRootObject: index.html line.
aws cloudfront get-distribution-config --id EDFDVBD6EXAMPLE --query "DistributionConfig.DefaultRootObject"The command prints the current value, so you can confirm it.
Why a missing file returns 403
With OAC and a bucket policy that grants only s3:GetObject, CloudFront has no permission to list the bucket. When a key does not exist, S3 cannot tell "missing" from "forbidden" without that permission. It returns 403 Forbidden instead of 404 Not Found. This is intentional, since it stops outsiders from probing which keys exist.
So a mistyped URL on a private bucket usually shows a 403 from CloudFront. If you also grant s3:ListBucket on the bucket ARN, S3 returns a real 404. Most sites leave the permission off and handle both codes.
Custom error responses
A custom error response tells CloudFront what to return when the origin answers with a given HTTP error code. Each rule has these fields:
| Field | Meaning |
|---|---|
| Error code | The origin status code to match, such as 403 or 404 |
| Response page path | The object to serve instead, such as `/error.html` |
| Response code | The status code sent to the viewer |
| Error caching minimum TTL | How long CloudFront caches the error, in seconds |
The response page path is fetched from the same origin, so error.html must exist in the bucket. CloudFront checks cache behaviors to decide where to fetch it, so the path must match a behavior that can serve it.
Here is a CloudFormation fragment for the DistributionConfig section:
CustomErrorResponses:
- ErrorCode: 403
ResponsePagePath: /error.html
ResponseCode: 404
ErrorCachingMinTTL: 60
- ErrorCode: 404
ResponsePagePath: /error.html
ResponseCode: 404
ErrorCachingMinTTL: 60Both rules show the same page and return 404 to the viewer. Browsers, crawlers, and monitoring tools then see the status that matches what happened.
Single-page applications
A single-page application (SPA) handles routes like /account/settings in JavaScript. No such file exists in S3, so a direct visit or a refresh produces a 403. The usual fix is to return the app shell instead:
CustomErrorResponses:
- ErrorCode: 403
ResponsePagePath: /index.html
ResponseCode: 200
ErrorCachingMinTTL: 0
- ErrorCode: 404
ResponsePagePath: /index.html
ResponseCode: 200
ErrorCachingMinTTL: 0The router in the browser then renders the right view. The trade-off is that real missing files, such as a broken image URL, also return index.html with status 200. Keep asset paths on a separate behavior or handle that case in the app.
Common errors
- Error responses stick around. CloudFront caches errors for the error caching minimum TTL, 10 seconds by default. If you upload a missing file, viewers may keep seeing the error until that time passes. Keep the value low while you work.
- The error page itself is missing. If
/error.htmlis not in the bucket, the viewer gets a plain error instead. - Root works, subfolders do not. This is the default root object limit described above.
Exercise
Your site uses OAC and a bucket policy with only s3:GetObject. A visitor opens /missing.png, which does not exist. What status does S3 return to CloudFront, why, and how would you make the visitor see your own error page with a 404 status?
Show answer
S3 returns 403 Forbidden, because without s3:ListBucket it cannot reveal whether a key is missing. Add a custom error response for error code 403 with response page path /error.html and response code 404, and make sure error.html exists in the bucket.
Exercise
A visitor to https://example.com/docs/ gets an error even though docs/index.html exists in the bucket and the default root object is index.html. Why?
Show answer
The default root object applies only to the root URL (/). A request for /docs/ is not rewritten, so CloudFront asks S3 for the key docs/, which does not exist. Request /docs/index.html explicitly or rewrite the URL, for example with a CloudFront Function.