Rails app with multiple domains

901 Views Asked by At

Im getting stuck at this. I have a Rails App, that running on DO droplet and over 10 domain, Nginx as web server and Puma. The difference between websites is style for each domain and their db. So i cant figure out how to make it works. As i can see, I should create a separate socket for every site and configure redirection.

Locally it is working like: in .env file i define some site i.e. domain1.comand app choosing certain style and. Sorry for mistakes, I maybe wrong, im not a developer, i just need to deploy it. At this time it is hosting on Heroku, separate app for separate site.

2

There are 2 best solutions below

1
stef On

You can create different tasks for different domains. Each task would just set the appropriate variables.

Take a look at https://github.com/mina-deploy/mina/blob/master/docs/cookbook.md#multi-environment-deploy

0
Nicholas Dill On

Rails makes it easy to handle multiple domains with just the routes.rb file. You can add constraints to your routes such that those endpoints and controllers only respond to requests made to a certain domain.

Constraints could look like this:

constraints(ShopDomain) do
  resources :shop
  get '/subscription', to: 'shop#subscription'
  root to: 'shop#index'
end

You could define constraints such as if a domain or subdomain matches a string. That could look like this:

class ShopDomain
  def self.matches? request
    request.subdomain == 'shop'
  end
end

Now in your case, each site has its own controllers that have their own views and styles.

I wrote a more in-depth description of this strategy on my blog, here.