As per this Amazon's article I was trying to make the whole WordPress website work behind AWS CloudFront. Not just the static files but the whole website (it can be done with setting up proper cache behaviors). However if you use the bare domain (example.com
, without www
) it seems that's impossible.
Namely, if the origin of a CloudFront distribution is example.com
, and if you put a CNAME for example.com
in that same distribution CloudFront will sporadically produce 403 Error. After some digging I found out that this is expected behavior since with this setup, where origin and the CNAME values are the same, CloudFront will look for the origin onto itself and produce error.
So how can one use naked domain and use CloudFront as a proxy at the same time?
Update:
I've implemented the origin.example.com
solution suggested in the comments. I was getting an error but now it works.
- The origin in the CF distribution is
origin.example.com
. - CNAME in the CF distribution is
example.com
. - In the Cache Behavior settings in the CF distribution the
Host
header is whitelisted. - In DNS
origin.example.com
points to the server's IP with an A record. - In DNS
example.com
points to the CF distribution with an ALIAS-A record.
The only beef I have with this workaround is that this way the origin server's IP address is discoverable on the net. A script kiddie can accidentally access origin.example.com
and the server's true IP address is in the open, thus you're prone to DDoS. One of the many benefits of a proxy is that with it you're hiding the true server's IP address.
I'm currently using Cloudflare as a proxy mainly because of that reason. In the past I was hit with massive DDoS attack and my server's IP address was null-routed by the host, so I had to quickly hide behind Cloudflare and change the server's static IP. No headaches since then. I wanted to switch to CloduFront but using the bare domain seems not viable.