Redirect www to non-www and FTP problem

I installed ghost on a newly installed VPS server, following the default guide.

I noticed that the domain.com is correct, it is the main one and takes me to the ghost site.

If I connect to www.domain.com, it takes me to the nginx welcome page.
Shouldn’t it take me to domain.com without the www? (as I wish it were)

The dns records are set up like this:

A - domain.com - IP
CNAME - www - domain.com
CNAME - ftp - domain.com

EDIT:
I noticed that ftp.domain.com takes me to the site, keeping the domain ftp.domain.com, instead of redirecting domain.com.
There is something wrong.

these are the two files that the ghost installation generated for nginx:
domain.com.conf

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

    server_name domain.com;
    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;
}

domain.com-ssl.conf

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name domain.com;
    root /var/www/ghost/system/nginx-root;

    ssl_certificate /etc/letsencrypt/domain.com/fullchain.cer;
    ssl_certificate_key /etc/letsencrypt/domain.com/domain.com.key;
    include /etc/nginx/snippets/ssl-params.conf;

    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;
}

What am I doing wrong?
Thank you

I solved it by adding these directives in both files:

if ($host = www.domain.com) {
    return 301 https://domain.com$request_uri;
} 

if ($host = ftp.domain.com) {
   return 301 https://domain.com$request_uri;
} 

And modified param server_name in both file:

server_name domain.com www.domain.com ftp.domain.com;

I’d like to know if it’s the right configuration as google has indexed hundreds of urls with ftp.domain.com. I would not like to create URL and traffic problems or be penalized on Google.

An expert reply would be appreciated

Thank you

1 Like

Hi @giacomosilli, I’m pleased you solved this however I think it would be better to manage domain redirects using controls at domain level. Looks like you were doing the right thing with the CNAME, and maybe you just needed to wait for that to resolve? It can take up to 24 hours in some cases.

This is just a suggestion, managing your domains at domain level would make more sense in my eyes :slight_smile:

Hello @DavidDarnes

You are absolutely right, I also prefer to solve the problem directly on the DNS. The changes seemed to be propagated almost immediately, in fact I encountered several problems when I changed them almost immediately.

For the moment I have solved this way. It seems that the results on google with the ftp.domain.com domain are decreasing, albeit slowly. As soon as there are no more I’ll try to edit the CNAME again

Thank you

1 Like