NginX multiple domains NOT working

domaindomain-name-systemnginx

In my DigitalOcean droplet, I'm trying to use my NginX to host multiple websites on the same droplet.
I can access test.domain1.com and domain1.com with or without the use of default_server tag. However, accessing domain2.com just presents the contents of domain1.com (the server block with the default_server tag) without forwarding the url to domain1.com in the browser.
I've checked for typo in my configs but can't find one. Secondly, neither the access.log nor the error.log have any issues.

The hostname of my droplet is domain1.com.
I've created 2 DNS entries: 1 for domain1.com and the other for domain2.com. Both have the same A and CNAME records.


@ A ip.add.re.ss
www CNAME @
* CNAME @

My configs are:
/etc/nginx/nginx.conf:
http://pastebin.com/AWf5EM66
/etc/nginx/global/wordpress.conf:
http://pastebin.com/Uc9KSqhh
/etc/nginx/global/restrictions.conf:
http://pastebin.com/cLfMv6jC

/etc/nginx/sites-available/sites.conf:

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

    root /usr/share/nginx/html/domain1_test;
    index index.php index.html index.htm;

    server_name test.domain1.com;

    include global/restrictions.conf;
    include global/wordpress.conf;

}

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

    root /usr/share/nginx/html/domain2.com;
    index index.php index.html index.htm;

    server_name www.domain2.com;

    include global/restrictions.conf;
    include global/wordpress.conf;
}

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

    root /usr/share/nginx/html/domain1.com;
    index index.php index.html index.htm;

    server_name www.domain1.com;

    include global/restrictions.conf;
    include global/wordpress.conf;
}

Any clues?

Best Answer

This is because you do not have a server with the domain2.com as server_name, only www.domain2.com. Either add domain2.com as server_name to the same server as www.domain2.com, either create another server for domain2.com that redirects to www.domain2.com using return.