简体   繁体   English

一个域,Heroku上的两个rails应用程序

[英]One domain, two rails apps on Heroku

I am building a multi-tenant Rails 5 app (will be hosted at heroku) at the moment (using apartment gem) which will have clients scoped by subdomain, eg client1.example.com, client2.example.com, client3.example.com. 我正在构建一个多租户Rails 5应用程序(将在heroku上托管)(使用公寓gem),其客户端将由子域定义,例如client1.example.com,client2.example.com,client3.example。 COM。 All seems pretty straight forward, but I would love to have a second Rails app for the rest of the site eg example.com/pricing, example.com/faq. 所有这些看起来都非常简单,但我希望为网站的其余部分安装第二个Rails应用程序,例如example.com/pricing,example.com/faq。 What would be the best solution to achieve this design: 实现此设计的最佳解决方案是什么:

  • example.com/pricing - served by Rails App 1 example.com/pricing - 由Rails App 1提供服务
  • example.com/faq - served by Rails App 1 example.com/faq - 由Rails App 1提供服务
  • example.com/{sign_in | example.com/{sign_in | sign_up} - served by Rails App 2 sign_up} - 由Rails App 2提供服务
  • clientX.example.com/{projects | clientX.example.com/{projects | profiles | 个人资料| other stuff} - served by Rails App 2 其他东西} - 由Rails App 2提供服务

I have a suspicion that sign in/up bit might not be possible due to same URL structure, so I might need to have an extra logic built it for it maybe under app.example.com/sign_up or even under Rails App 1. Any suggestion how to build this in a better way would be appreciated! 我怀疑由于相同的URL结构可能无法登录/升位,所以我可能需要为app.example.com/sign_up或甚至在Rails应用程序1下构建一个额外的逻辑。任何建议如何以更好的方式建立这一点将不胜感激!

Don't think you can mount two apps under the root directory. 不要以为你可以在根目录下挂载两个应用程序。 You might be able to achieve this if you're willing to namespace your Devise paths under a subdirectory, eg. 如果您愿意在子目录下命名您的Devise路径,例如,您可能能够实现此目的。 /session/sign_up and /session/sign_in /session/sign_up/session/sign_in

This will require an extra app (App 3) which just runs Devise loaded and deployed under the /sessions subdirectory. 这将需要一个额外的应用程序(应用程序3),它只运行在/sessions子目录下加载和部署的Devise。 You'll need to configure the relative url root for this app. 您需要为此应用配置相对网址根目录

This will make authentication more complex as your session authentication cookie will be under your root domain instead of your individual subdomains. 这将使身份验证更加复杂,因为您的会话身份验证cookie将位于根域而不是您的单个子域下。 You'll probably need to use the domain: :all or domain: .<domain>.com option in initializers/session_store.rb to share the cookie with subdomains. 您可能需要在initializers/session_store.rb使用domain: :alldomain: .<domain>.com选项来与子域共享cookie。 secret_key_base also needs to be identical across all your apps. secret_key_base也需要在所有应用中保持一致。

You'll also need to play with Devise's session creation to redirect to the correct subdomain for the user upon signing in. Fun fact to think about - what if the user has accounts on multiple subdomains with the same email? 您还需要使用Devise的会话创建,以便在登录时重定向到用户的正确子域。有趣的事实要考虑 - 如果用户在具有相同电子邮件的多个子域中拥有帐户,该怎么办? Devise/Warden scopes might offer a way to handle that. Devise / Warden范围可能提供一种处理方式。

On the Heroku side: 在Heroku方面:

Heroku can only route requests for one domain to one app (App 1). Heroku只能将一个域的请求路由到一个应用程序(App 1)。 The Rack reverse proxy posted above will allow you to route authentication requests to App 3. For individual subdomain requests to be routed to App 2, you'll need to add each subdomain to App 2 as a separate custom domain. 上面发布的Rack反向代理将允许您将身份验证请求路由到App 3.要将各个子域请求路由到App 2,您需要将每个子域作为单独的自定义域添加到App 2。

有点担心这将关闭“过于宽泛”,但这里的一个简单的解决方案是做heroku domains:add www.example.comheroku domains:add example.com到Rails app 1.然后heroku domains:add *.example.com在Rails应用程序2上的heroku domains:add *.example.com ,并在Heroku上有两个完全独立的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM