Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple domains pointing to single Heroku Rails app via nameservers displaying different content?

We have a Rails 3 app which allows users to create a profile and access it with a subdomain. So for example:

  • user1.app.com
  • user2.app.com
  • user3.app.com

Now, suppose they wanted to point their own domain name to their profile, so www.user1.com shows user1.app.com, and www.user1.com/my-content/ shows user1.app.com/my-content/. Is it possible for them to simply change their nameservers to point to us, and we handle the routing? I'm afraid it would be a deal breaker if the user had to do any DNS configuration beyond just changing their nameservers.

Thanks!

like image 597
Derek J Avatar asked Jun 15 '11 16:06

Derek J


2 Answers

This is really two questions. So I guess I'll answer it like that.

How can I allow customers to add a custom domain to my heroku-based app?

So, in your example, their domain name is www.user1.com

Two things need to happen

  1. www.user1.com needs to be added to your heroku domains for the app
  2. the dns for www.user1.com needs to be set to the heroku servers

In order to add this new domain, I guess you need to store the custom domain in the app, and when it is changed, or created, ensure a queued task is setup to push this config change to heroku.

In order to do the dns correctly, I hope it is enough that they add a CNAME for user1.app.com. Therefore, if you have a wildcard CNAME linking to heroku's dns, you can avoid the type of downtime that can occur if Heroku gets DOS-ed again.

How can I show content based on domain, rather than just subdomain?

Well, somehow you already differentiate user1.app.com from user2.app.com

Maybe you just sniffed request.host. Now you just have to try the custom domain first.

Maybe this'll work.

before_filter :set_client

def set_client
  @client = Client.find_by_custom_domain(request.host) || Client.find_by_subdomain(request.host.split(".").first)
  unless @client
    raise "unrecognised domain #{request.host}"
  end
end
like image 122
Matthew Rudy Avatar answered Sep 21 '22 17:09

Matthew Rudy


You need to get them first of all have them set www.user1.com to point to proxy.heroku.com (or setup your own cname entry to proxy.heroku.com and get them to cname their domains to that) and then you need to register their custom domain with your heroku app so the correct application responds at Heroku stored against their account?? in your application so it can be matched on incoming requests.

This technique is employed in the Locomotive Rails CMS so I'd suggest looking at that - it uses the Heroku gem INSIDE the application so custom domains can be added via the site administrator which in turn talks to the Heroku API to add the custom domain to the application.

like image 13
John Beynon Avatar answered Sep 22 '22 17:09

John Beynon