我在Apache上有两个域:domain1.tld
&domain2.tld
目前,我的domain1.tld
与Passenger完美配合,但我也需要domain2.tld
来指向同一个应用程序,但当它到达第二个域时,它将具有与用户点击domain1.tld
时不同的功能(即,调用不同的控制器并具有一些不同的路由)。
传统上在Rails中是如何配置的?
就Apache配置而言,我对domain1.tld
有以下内容:
DocumentRoot /home/username/apps/domain1.tld/production/current/public
<Directory /home/username/apps/domain1.tld/production/current/public>
AllowOverride all
Options -MultiViews
Require all granted
</Directory>
ErrorLog /home/username/logs/domain1.tld.error.log
CustomLog /home/username/logs/domain1.tld.access.log combined
Apache配置的domain2.tld
需要什么?
您的设置在某个时候也可能演变为拥有两个完全独立的应用程序。我会让你更好地判断何时不需要这样做。话虽如此,您可以使用Rails中的路由约束来实现您所要求的。例如:(请注意,这不是实现这一目标的唯一方法,而是我喜欢的方法)
root to: 'home#index1', as: :domain1, constraints: {|req| req.host == 'domain1.tld' }
root to: 'home#index2', as: :domain2, constraints: {|req| req.host == 'domain2.tld'}
root to: 'home#index'
要添加新的Apache主机,需要添加另一个虚拟主机并指向与主应用程序相同的公用文件夹。因此,在这种情况下,只需创建与第一个配置文件中相同的所有信息,但只需添加domain2.tld
作为新虚拟主机的名称。