- Newest
- Most votes
- Most comments
Hello.
What is the folder structure of S3?
When you access "https://mymainapp.example.com/app1/index.html", a request is sent to "app1/index.html" in S3.
In other words, if the file is not located in "app1/index.html" of app1-content, an error will occur and you will not be able to access it.
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesOriginPath
If you want CloudFront to request your content from a directory in your origin, enter the directory path, beginning with a slash (/). CloudFront appends the directory path to the value of Origin domain, for example, cf-origin.example.com/production/images. Do not add a slash (/) at the end of the path.
For example, suppose you’ve specified the following values for your distribution:
- Origin domain – An Amazon S3 bucket named amzn-s3-demo-bucket
- Origin path – /production
- Alternate domain names (CNAME) – example.com
When a user enters example.com/index.html in a browser, CloudFront sends a request to Amazon S3 for amzn-s3-demo-bucket/production/index.html.
When a user enters example.com/acme/index.html in a browser, CloudFront sends a request to Amazon S3 for amzn-s3-demo-bucket/production/acme/index.html.
The issue you're experiencing is likely due to a combination of factors, including the error page configuration and potentially the order of your cache behaviors. Here are some steps to troubleshoot and resolve the problem:
-
Check the order of your cache behaviors: Ensure that the new behavior for "/app1/" is listed before the default () behavior in your CloudFront distribution settings. CloudFront evaluates behaviors in order, so if a more general pattern comes first, it may override your specific "/app1/*" behavior.
-
Verify the Origin Access Control (OAC) settings: Make sure the OAC for the new origin (app1-content bucket) is correctly configured and associated with the distribution.
-
Double-check the S3 bucket policy: Ensure that the policy for the app1-content bucket correctly grants access to the CloudFront distribution. The policy should include the specific ARN of your CloudFront distribution.
-
Review the error page configuration: While redirecting errors to index.html can be useful for single-page applications, it may interfere with serving content from multiple origins. Consider modifying the error page configuration to be more specific or removing it temporarily for testing.
-
Check CloudFront logs: Enable logging for your CloudFront distribution if not already done, and review the logs to see if there are any access denied errors or other issues when accessing the /app1/* path.
-
Verify the path pattern: Ensure that the path pattern "/app1/*" is correctly entered in the behavior settings and matches the structure of your app1-content bucket.
-
Clear CloudFront cache: After making changes, you may need to invalidate the CloudFront cache to ensure the new settings take effect immediately.
If after checking these points the issue persists, you might want to consider using Lambda@Edge or CloudFront Functions to have more granular control over request routing between your origins.
Remember that changes to CloudFront distributions can take some time to propagate, so allow some time after making changes before testing again.
Sources
How can I connect multiple s3 static apps to one Cloudfront and direct traffic to the different s3 based on path | AWS re:Post
Add, remove, or replace content that CloudFront distributes - Amazon CloudFront
cloudfront distribution with several origins | AWS re:Post
Relevant content
- asked 4 years ago
- asked 7 months ago
- AWS OFFICIALUpdated 3 months ago

All the files are at the root.
If everything is placed in the root directory, I think it can be resolved by placing it as 'app1/index.html'. Alternatively, you will need to create some code, but I think it is possible to rewrite the origin request URL using Lambda@Edge. https://aws.amazon.com/jp/blogs/networking-and-content-delivery/implementing-default-directory-indexes-in-amazon-s3-backed-amazon-cloudfront-origins-using-lambdaedge/
Thanks Riku Kobayashi! SO thread you provided in the other thread helped me. I ended up creating Lambda Function to strip the path and now it works.