multi tenant with custom domain on rails
Asked Answered
D

2

4

I'm creating a multi tenant application like shopify and wanna know how can I create custom domain on server that points to the same application instance? For example:

app1.mysystem.com == www.mystore.com
app2.mystem.com == www.killerstore.com

I need to do that config on CNAME like Google Apps? If so, how can I do that? Is there some good paper showing how this works ?

PS: app1 AND app2 points to the same application! Thanks

Dollie answered 25/7, 2011 at 20:12 Comment(0)
R
7

I have a similar setup and am using nginX. What I did for ease of maintenance was accepted all the connections from nginx and did the filtering in my app.

# application_controller.rb
before_filter :current_client

private
def current_client
  # I am using MongoDB with Mongoid, so change the syntax of query accordingly
  @current_client ||= Client.where(:host => request.host).first
  render('/public/404.html', :status => :not_found, :layout => false) unless @current_client
end

You can have your clients have a domain record with there domain/subdomain pointing to you_ip or your_domain_pointing_to_your_ip.com and capture that in a form and save in database. Then alter the query in current_client like:

@current_client ||= Client.or(:host => request.host).or(:alias => request.host).first
Recess answered 25/7, 2011 at 20:40 Comment(1)
Trying to do the same thing but not 100% sure how to accept all connections. Can you post your nginx conf?V1
W
1

I am currently working through something similar and just did the Nginx configuration. This is the way that I did it.

server {
  listen 80;
  server_name domain1.com domain2.com domain3.com;
  rails_env production;
  passenger_enabled on;
  root /var/www/your_site_folder/current/public;
}

Also make sure to run passenger_pre_start if you are using passenger.

ex: passenger_pre_start http://your_domain.com;

Add one line for each domain that you add into the above config block.

The key here is under server_name. Normally I would use this for a domain using www.domain.com or without the 'www', domain.com. But in this case you can point all domains that you want to hit your app from here and you have your Nginx setup for multi-tenancy.

Warram answered 6/4, 2016 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.