使用标准的虚拟主机配置,您可以执行以下操作:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName app.example.com
ServerAlias *.example.com
DocumentRoot /web/app.example.com/public
</VirtualHost>
这将捕获其他虚拟主机条目尚未捕获的所有请求。
当应用程序收到请求时,您将使用提供的主机名设置请求变量。这可用于任何操作控制器:
request.host
从那里,您可以在某种类型的“筛选前”中加载适当的数据,通常这样做:
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
只要客户机正确地填充了主机名,就可以在每次页面加载时找到它们。