I was really struggled so I leave it for anyone else facing the same issue.
Document:
https://docs.railway.app/tutorials/add-a-cdn-using-cloudfront
My 1st solution:
https://help.railway.app/questions/how-i-can-add-cloud-front-671b8863
The approach described in the documentation, which doesn't forward viewer headers to the origin server, caused issues as the application identified the traffic as coming directly from Railway, resulting in some processing problems. While this setup worked fine with simple Node.js backend APIs, when deploying Laravel applications, I encountered issues where asset URLs and Inertia link domains were incorrectly set to Railway's domain.
While the asset URLs could potentially be fixed using ASSET_URL, the Inertia-related issues seemed more challenging to resolve. I was looking for a way to forward the original viewer's headers while ensuring requests would work properly.
Solution
- Set up a Custom Domain (e.g. xxx.example.com) in Railway
- Ignore the CNAME DNS settings shown in Railway
- Copy the generated CNAME value (youroriginal.up.railway.app)
- Create a Cloudfront distribution and set youroriginal.up.railway.app as the origin
- When configuring the Behaviour settings, set the Origin request policy (optional) to "AllViewer". This is necessary to set All viewer headers to Headers section.
- Configure your DNS settings to create a CNAME record pointing your custom domain (xxx.example.com) to your Cloudfront distribution domain (xxx.cloudfront.net)
- You should now be able to connect through your custom domain
- You can ignore the "Waiting for DNS" status in Railway
Top comments (0)