DEV Community

Olimpio
Olimpio

Posted on

How can I share the same domain with two different servers?

Is it possible to have two separate servers share the same domain name? If so, how?

I have a website and email services hosted on a nginx server with the domain name site.co.mz, working perfectly.

But recently I'm developing a new website that should replace the one hosted on this nginx server. I've hosted this new website on Heroku because I'm developing this website using Jekyll and some gems which Heroku makes it easy to host and it's working perfectly too.

I want to know if is it possible to share the same domain name site.co.mz on these two different servers?

Top comments (8)

Collapse
 
hexrcs profile image
Xiaoru Li • Edited

What you are looking for is something like a load balancing mechanism.

One way to do this is you have a 3rd load balancing server which the domain actually should point to. It works as a proxy and "firewall" that forwards the user requests to your old nginx site or the new heroku site. Exactly which user ends up seeing which site can be programmable.

Another way is to use a DNS server to resolve the domain to different IPs, so your users actually directly visit different servers. I haven't done this before but if you ask your domain registrar or DNS provider (eg. cloudflare) you should be able to find more info how to set it up.

Collapse
 
rembou1 profile image
remi bourgarel

The dns solution doesn't work well from.my experience and is not supported by every client

Collapse
 
hexrcs profile image
Xiaoru Li • Edited

Do you mean not supported by every DNS provider out there? It's true in that case. I don't have experience doing this myself but I believe most standalone DNS service providers have this feature, if the default DNS servers the domain registrar assigned for you do not.

All the major sites and CDN out there use this technique to do load balancing. That's why you get different IPs for google.com from different places.

Thread Thread
 
rembou1 profile image
remi bourgarel

No, I guess every dns provider manages it but it needs to be also implemented at the browser level : if the resolved address doesn't answer on port 443 then try the next address. I use this only for internal service for which I don't want to pay for load balancer

Thread Thread
 
hexrcs profile image
Xiaoru Li

I see what you mean. However the availability checking should be done by the load balancer, not the client side.

The client simply asks a DNS server to resolve a domain to an IP so it can access it; the DNS server then answers the client with an IP of a server that's optimal for the client (based on location, server load etc). But normally there are layers of load balancing going on and you combine these two types of methods, so the DNS server in that case returns the IP of a load balancer server, and the load balancer server relays/forwards the user requests to the actual, hidden end servers.

Thread Thread
 
rembou1 profile image
remi bourgarel

I was answering to your second solution where the dns resolves to multiple ip. A ha loadbalancer is a must have if you need to send load to multiple servers.

Collapse
 
devdrake0 profile image
Si

Yes and no...

When you setup your CNAME on your hosting provider, you have to give it a public IP address.

That IP address could be to a load balancer, that distributes the traffic between two servers. I don't think this is what you want.

You can setup subdomains, though. You could point site.co.mz to one server and new.site.co.mz to the server you're developing on. Once you're happy with the new site, you can switch site.co.mz to the new server.

You cannot point one CNAME to two IP addresses, no.

Does that make sense?

Collapse
 
rnrnshn profile image
Olimpio

Thank you. With all your replies now I can at least think of how I can solve this... ๐Ÿ”ฅ