Apex domain redirect to www Route 53

Viewed 319

Currently, I have my website hosted on a CentOS server running Nginx.

I have two domains, example.com and example.org, and I wish to redirect all traffic from example.com to example.org and have managed to make it work for both www.example.org (serving the website correctly) and www.example.com (redirecting to www.example.org).

I've been trying to set this up for a while now, but still haven't been able to redirect from example.com to www.example.org.

On Route, I have a CNAME www record, which points to my EC2 instance, but as the same can't be done to the apex record, I've researched and decided to create a Load Balancer to my EC2 instance, and the apex record point to the newly created load balancer, using the DNS name in the load balancer description.

Pasting the load balancer DNS name on my browser correctly loads my website, but hitting example.com returns an Nginx 404 error page.

On Route 53, I have:

Record name        Type     Alias    Value/Route traffic to
example.com        A        Yes      lb.to.ec2.instance.amazonaws.com        (DOESN'T WORK, Nginx 404)
www.example.com    CNAME    No       ec2.instance.compute-1.amazonaws.com    (WORKS)

And on my server's Nginx config, I have

server {
    listen 80;

    server_name example.com lb.to.ec2.instance.amazonaws.com;

    return 301 https://www.example.org$request_uri;
}

I've checked Nginx access logs and confirmed that it was my server that returned the 404 error. Am I missing something? I even tried

server {
    listen 80;
    return 301 https://www.example.org$request_uri;
}

Just to make sure it would redirect to the correct address every time, still no luck.

1 Answers

If you have load balancer this seem overcomplicated.

Points all DNS records (incl apex ones as alias) at the load balancer and take care of the redirection over there.

enter image description here

Then replace

server_name example.com lb.to.ec2.instance.amazonaws.com; from your Nginx with server_name _

Related