将请求从域发送到特定的Rails引擎 [英] Send Requests from Domain to Specific Rails Engine
问题描述
我在Rackspace VPS上运行了一个Rails应用程序.堆栈是rails3 + unicorn + nginx + mysql.
I have a single rails app running on a Rackspace VPS. The stack is rails3 + unicorn + nginx + mysql.
There is a primary domain that sends traffic directly to the unicorn socket using proxy_pass
.
我开发了一个安装在/digital
下的新引擎.现在,人们可以通过 http://primarydomain.com/digital 与该引擎进行交互.
I have a new engine developed that's mounted under /digital
. Right now, people can interact with that engine via http://primarydomain.com/digital.
我想托管一个新域,该域将请求直接转发到/digital
;而不是根引擎.
I want to host a new domain that forwards requests directly to /digital
; not to the root engine.
因此,例如,以下请求将产生等价的结果:
So for instance, the following requests would yield equivilent results:
http://primarydomain.com/digital/splash
http://alternatedomain.com/splash
在理想世界中,引擎将是一个单独的应用程序.尽管它实际上是一个已安装的引擎,但我想将其视为单独的域是一个单独的应用程序.
In a perfect world, the engine would be a separate app. I want to act as if the separate domain is a separate app although it's really a mounted engine.
routes.rb
的外观如下:
Company::Application.routes.draw do
root :to => 'spree/home#splash'
ActiveAdmin.routes(self)
mount Core::Engine, :at => '/'
mount Another::Engine, :at => '/digital'
end
要使此功能正常工作,我需要哪些rails + nginx配置?
What rails + nginx configuration do I need to get this working?
推荐答案
一种简单的解决方法是在Rails中使用路由约束.实际上,将应用程序的各个部分包含在引擎中使其变得更加简单.
A simple way to solve this is using route constraints within Rails. In fact, having the parts of your app in engines makes it even simpler.
Company::Application.routes.draw do
mount Core::Engine, at: '/', constraints: { domain: 'coredomain.com' }
mount Another::Engine, at: '/', constraints: { domain: 'anotherdomain.com' }
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
end
确保引擎安装架在路线中排在首位,以便它们的路线优先于ActiveAdmin
添加的内容.
Make sure the engine mounts come first in the routes so that their routes take preference over whatever ActiveAdmin
adds.
现在,您只需要使用nginx将两个域都指向该应用程序,就可以了.
Now you just need to point both domains at the app with nginx and you're good to go.
如果您也想使它在coredomain.com/digital
下工作,您甚至可以挂载两次Another::Engine
:
You may even mount Another::Engine
twice if you want to keep it working under coredomain.com/digital
too:
Company::Application.routes.draw do
constraints domain: 'coredomain.com' do
mount Core::Engine, at: '/'
mount Another::Engine, at: '/digital'
end
constraints domain: 'anotherdomain.com' do
mount Another::Engine, at: '/'
end
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
end
这篇关于将请求从域发送到特定的Rails引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!