Configuring multiple domains and subdomains. DNS & Nginx issues abound!

October 27, 2014 129.3k views

What I'm shooting for:

  • domain1.com > /home/user/www/domain1
  • domain2.com > /home/user/www/domain2
  • sub1.domain2.com > /home/user/www/sub1
  • sub2.domain2.com > /home/user/www/sub2
  • drop.let.ip.address > /home/user/www/domain1

What is happening:

  • Navigating to drop.let.ip.address redirects to domain2
  • Navigating to domain1 redirects to domain2
  • Navigating to domain2 fails to resolve host
  • Navigating to sub1 does not redirect, but fails to resolve host
  • Navigating to sub2 does not redirect, but fails to resolve host

ZONE file for domain1:

$TTL    1800
@       IN  SOA NS1.DIGITALOCEAN.COM.   hostmaster.domain1.com. (
            1414393689 ; last update: 2014-10-27 07:08:09 UTC
            3600 ; refresh
            900 ; retry
            1209600 ; expire
            1800 ; ttl
            )
             IN      NS      NS1.DIGITALOCEAN.COM.
                     NS      NS2.DIGITALOCEAN.COM.
                     NS      NS3.DIGITALOCEAN.COM.
@   IN A     drop.let.ip.address

ZONE file for domain2:

$TTL    1800
@       IN  SOA NS1.DIGITALOCEAN.COM.   hostmaster.domain2.com. (
            1414403298 ; last update: 2014-10-27 09:48:18 UTC
            3600 ; refresh
            900 ; retry
            1209600 ; expire
            1800 ; ttl
            )
             IN      NS      NS1.DIGITALOCEAN.COM.
                     NS      NS2.DIGITALOCEAN.COM.
                     NS      NS3.DIGITALOCEAN.COM.
@   IN A     drop.let.ip.address
*.sub1 IN A drop.let.ip.address
*.sub2 IN A drop.let.ip.address

Server blocks for domain1:

server {  
    server_name  domain1.com;  
    rewrite ^(.*) http://www.domain1.com$1 permanent;  
}  

server {  
        listen 80 default_server;  
        listen [::]:80 default_server ipv6only=on;  

        root /home/user/www/domain1;  
        index index.php index.html index.htm;  

        server_name www.domain1.com;  

        include hhvm.conf;  

} 

Server blocks for domain2:

server {
    server_name  domain2.com;
    rewrite ^(.*) http://www.domain2.com$1 permanent;
}       

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

        root /home/user/www/sub1;
        index index.html index.htm index.php;

        server_name sub1.domain2.com;

        include hhvm.conf;
} 

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

        root /home/user/www/sub2;
        index index.html index.htm index.php;

        server_name sub2.domain2.com;

        include hhvm.conf;
}

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

        root /home/sub/www/domain2;
        index index.html index.htm index.php;

        server_name www.domain2.com;

        include hhvm.conf;
}

I've been pulling my hair out all day, and I'm pretty sure the reason it's so hard to google is because it's a simple DNS issue and I have no idea what i'm doing with DNS.

Any help would be greatly appreciated.

2 Answers

First you need to fix domain2. Delete the records of sub1 and sub2 and create CNAME records for them.

name: sub1, hostname: @
name: sub2, hostname: @

You probably want to handle www sub domains too so add a CNAME record the same way to domain1 and domain2, but this time use www as the name of the record.

If you want to handle www prefixes on your sub1 and sub2 sub domains then add another two CNAME records to domain2.

name: www.sub1, hostname: sub1
name: www.sub2, hostname: sub2

Your server blocks should look like this.

# www.domain1.com

server {
  listen 80;
  server_name www.domain1.com;
  rewrite ^(.*) http://domain1.com$1 permanent;
}

# domain1.com

server {
  listen 80 default_server;
  server_name domain1.com;
  root /home/user/www/domain1;
  index index.php index.html index.htm;
  include hhvm.conf;
}
# www.domain2.com

server {
  listen 80;
  server_name www.domain2.com;
  rewrite ^(.*) http://domain2.com$1 permanent;
}

# domain2.com

server {
  listen 80;
  server_name domain2.com;
  root /home/user/www/domain2;
  index index.php index.html index.htm;
  include hhvm.conf;
}

# www.sub1.domain2.com

server {
  listen 80;
  server_name www.sub1.domain2.com;
  rewrite ^(.*) http://sub1.domain2.com$1 permanent;
}

# sub1.domain2.com

server {
  listen 80;
  server_name sub1.domain2.com;
  root /home/user/www/sub1;
  index index.php index.html index.htm;
  include hhvm.conf;
}

# www.sub2.domain2.com

server {
  listen 80;
  server_name www.sub2.domain2.com;
  rewrite ^(.*) http://sub2.domain2.com$1 permanent;
}

# sub2.domain2.com

server {
  listen 80;
  server_name sub2.domain2.com;
  root /home/user/www/sub2;
  index index.php index.html index.htm;
  include hhvm.conf;
}

The default_server sets that block explicitly the default server. Droplet IP should be handled by that block. Make sure you don't have a conflicting default server.

  • Thanks for your help!

    rather than:

    name: sub1, hostname: @
    name: www.sub1, hostname: sub1
    

    Can I just do:

    name: *.sub1, hostname: @
    

    Would that capture foo.sub1.domain2.com and redirect to sub1.domain2.com?

  • That would match every non-existent domain ending with sub1.domain2.com. It would send foo.sub1.domain2.com to @ which is your droplet IP. I'm not sure that it would match sub1.domain2.com itself or not. It might do that if there were no record for sub1.domain2.com.

    If you want to redirect something to a sub domain using a CNAME record put the sub domain into the hostname field. The @ means the address of origin which is your droplet IP because of the A record of @.

  • It would worth trying out the wildcard method. Instead of having a dedicated CNAME for every sub domain, you could have a wildcard record for them.

    name: *.sub1, hostname: @
    

    You would have to modify server block www.sub1.domain2.com by changing server_name.

    server_name *.sub1.domain2.com;
    

    The question would be that whether it would work with sub1.domain2.com or not. You have to test it. Don't forget the TTL on the zone file changes. It could take an hour or so for a change to take effect.

    I would only do it this way if there is a need to handle arbitrary sub domains. If you only need to handle sub1 and www.sub1 then it would be better to just give them their own CNAME.

  • So. After some reflection-

    Since the subdomains are really only for staging anyway, I decided I don't need to prepend a wildcard and in fact don't even need to support www.

    In the end what worked is:

    $TTL    1800
    @       IN  SOA NS1.DIGITALOCEAN.COM.   hostmaster.domain2.com. (
                1414403298 ; last update: 2014-10-27 09:48:18 UTC
                3600 ; refresh
                900 ; retry
                1209600 ; expire
                1800 ; ttl
                )
                 IN      NS      NS1.DIGITALOCEAN.COM.
                         NS      NS2.DIGITALOCEAN.COM.
                         NS      NS3.DIGITALOCEAN.COM.
    @   IN A     drop.let.ip.address
    *       CNAME   @
    test    CNAME   @
    drop    CNAME   @
    

    With a server block using server_name sub1.domain2.com;, etc.

    It's always the little, stupid stuff that gets you :)

    Thanks for your help!

I have one doubt. I edit the server block at the same field or create a new server block?? for create a sub domain. Because I trying to create a sub domain for my blog 3hours.in but I could not be processed it. Because the www is not clear at the same time.

Have another answer? Share your knowledge.