1

I'm following two tutorials (1 and 2) to setup two domains on my server but after many attempts, I can't figure out how to have both domains working and www redirecting to non-www. At the moment, with the same server block files and DNS Records, both work in different ways, I think it could be some cache but I don't know why it is. The server block files are:

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

       server_name domain.com;

       root /var/www/domain.com/html;
       index index.html;

       location / {
               try_files $uri $uri/ =404;
       }
}

And both have the same DNS Records:

A Record | @ | VPS IP
A Record | www | VPS IP

And the problem is that domains don't work properly.

  • "domain1.com" don't work (DNS_PROBE_FINISHED_NXDOMAIN error on Chrome).
  • "www.domain1.com" redirects to the default page (the block server that is set as default) and not to the correct one (the one that is in the block server file of domain1)
  • "domain2.com" works fine.
  • "www.domain2.com" don't work (DNS_PROBE_FINISHED_NXDOMAIN).

I restarted nginx and the VPS but both still work in different ways (as I described).

JustAnotherCoder
  • 111
  • 3
  • 11
  • 1
    check this out https://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www – lotfio Aug 01 '17 at 01:02
  • Thanks, @Timino, I have already read that post in the past and it didn't work because I had another DNS Records, but now it works with one of the answers of that thread. – JustAnotherCoder Aug 01 '17 at 01:39

1 Answers1

0

I have finally found a way to do it.

It's a server block config that I have already proven but with other DNS (in the past), so now it is working with the following combination:

DNS Records (same as above):

A Record | @ | VPS IP
A Record | www | VPS IP

Server block config:

server {
        listen 80;
        listen [::]:80;
        server_name www.domain.com;
        return 301 $scheme://domain.com$request_uri;
}
server {
        listen 80;
        listen [::]:80;
        server_name domain.com;

        root /var/www/domain.com/html;
        index index.html;
}

Thanks to @Timilo for the suggestion that moved me to try it again.

JustAnotherCoder
  • 111
  • 3
  • 11