Multiple customer instances of an application, under a single application.

What I need is to allow multiple users, to connect to my Apache Web server, by passing different url like : customer1.myhost.com company1.myhost.com company2.myhost.com etc.

What I want my Apache server to do, is pass all request that are not directed to a certain list of existing hosts (like trac.myhost.com and https://myhost.com) over to my Rails application, by setting a RequestHeader to identify the requested host, something like :

RequestHeader "INSTANCE_NAME" = customer1 #for customer1.myhost.com

Thanks for your help!

Ps.: The end goal is to offer software slices as a service, but having all those customers managed under 1 application running. Not 1 app per customer.

Accepted Answer

Using a standard VirtualHost configuration you can do this:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName app.example.com
  ServerAlias *.example.com

  DocumentRoot /web/app.example.com/public
</VirtualHost>

This will capture all requests that are not already captured by other VirtualHost entries.

When your application receives the request, you will have the request variable set with the host-name provided. This is available to any ActionController:

request.host

From there you can load the appropriate data in some kind of before_filter, as is typically done like:

before_filter :load_client

def load_client
  @client = Client.find_by_hostname!(request.host)
rescue ActiveRecord::RecordNotFound
  render(:partial => 'client_not_found', :status => :not_found)
end

So long as the client has the hostname populated correctly, this will find them on each page load.

Written by tadman
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki