Question

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

What I’m shooting for:

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.


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.

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.