Score:0

Redirect domain root to CloudFront with nginx?

jp flag

Note - I see several other questions asking about redirecting to the root of their S3 static websites, but I didn't see anything about redirecting from the root of a domain name to a CloudFront/S3 site.

I have my personal website deployed to AWS CloudFront & statically hosted on S3. I have a www. CNAME record pointing to the CloudFront hostname, and that works properly, but I've discovered that you can't have a CNAME record for the domain root. I already have a VPS with nginx that's hosting several of my other projects - is it fine to simply add an A record from the domain root to my VPS IP, and have nginx redirect requests for the domain root to the CloudFront hostname?

Edit: to clarify - what I would like to happen is for all traffic heading to example.com be forwarded to the CloudFront hostname assigned to my website. I am able to do this with www.example.com by creating a CNAME record for 'www.' pointing to the desired hostname. However, it appears that Google Domains does not allow CNAME records to be created for the root (i.e. just example.com), and so I am wondering how to forward traffic from example.com to my CloudFront hostname (as opposed to what I can do right now, which is only to forward www.example.com to cloudfront).

And in case it's relevant, I can't do any sort of wildcard redirect because I am using other hostnames on the domain for different projects that require specific A records.

And would this be an acceptable/good practice nginx configuration for the site? I think it would work because www.example.com is already properly forwarding to the CloudFront hostname.

server {
        listen 443 ssl;
        server_name example.com;
        rewrite ^/$ https://www.example.com permanent;
        ssl_certificate           /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key       /etc/letsencrypt/live/example.com/privkey.pem;
}
Tim avatar
gp flag
Tim
I'm confused. Can you edit your question to describe your problem better, rather than just suggesting an end state?
the_midnight_developer avatar
jp flag
@Tim I think that's a bit clearer now
Tim avatar
gp flag
Tim
Not really. You're mixing together your assumptions, current state and target state, problem, and proposed implementation. For example, you mention a server and domains, but are they different domains, subdomains, or just directories off this domain? You also say "forward traffic", but that's implementation, it might just be a domain resolution. I suggest you delete this question and start again, writing it to make it easy for others. Suggest two sections "current state" and "target state", and make sure you cover what you're trying to achieve without any assumption about implementation.
Score:0
cn flag

By domain root, I assume you mean example.com, instead of www.example.com, and that you want to point that to your AWS cloud web app. One thing you can do is create an A record named @, which essentially says "this is me. Any DNS requests to me (example.com) should go to this IP address." Then add a CNAME www that points to @. You can use Nginx if you want, Nginx used for pass-through is just a reverse proxy. It will act as a middle man of any requests between the client and the server. Nginx just repeats those messages between the client and the server.

I hope that was any help, my feeling is I read the question wrong

the_midnight_developer avatar
jp flag
Ok I'm a little confused, with that setup, what is the purpose of the CNAME record pointing to @? Can't I just create the A record that points to my nginx server and do the forwarding like you said?
cn flag
Yeah. That would be perfectly fine. Just have it pass_thru to the server you want it to, and set your A record to the nginx server.
Score:0
cn flag

If you migrate your domain to AWS' Route53 service, this is very easy. You simply use the wizard to create an Alias record, which is a route53-specific feature. With an Alias you can point the zone apex (your example.com) to the Cloudfront Distribution, and then just create a second record that for www that also is an alias to the CF distribution.

Migrating between registrars is also quite simple - I did this myself from 123-reg to AWS last year and it was pretty much painless.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.