Question

Redirecting Traffic from www.domain.com to domain.com

I’ve decided to use the one-click Ghost install on digitalocean, and I like how easy it has made the process. However, I am having an issue with my site. I want to redirect the www.domain.com to domain.com, but my config doesn’t seem to work.

I have Letsencrypt certs for my domain which is arsalan.io, and it works fine. It gives a cert error if you try www.arsalan.io, so since I don’t want people browsing to www, I’d rather just have it forwarded to arsalan.io. I’ve setup www as a CNAME that points to the main domain.

My NGINX config is as follows (/etc/nginx/sites-available/arsalan.io.conf):

server {
    listen 80;
    listen [::]:80;

    server_name arsalan.io;
    root /var/www/ghost/system/nginx-root;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}

I’ve tried modifying it so it looks like the following but it doesn’t redirect:

server_name arsalan.io www.arsalan.io;
redirect 301 https://arsalan.io$request_uri;

I haven’t modified arsalan.io-ssl.conf


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Because you want to redirect a request from www.arsalan.io to arsalan.io you want to create a new server directive that is listening for connections on just www.arsalan.io and then add the redirect.

So you would need to update your configuration with the following:

server {
    server_name www.arsalan.io;
    return 301 $scheme://arsalan.io$request_uri;
}

In the snippet that you posted you had both server_names present, which may have been causing the issue.

You can find a more detailed write here: How to redirect with Nginx