简体   繁体   中英

Rails 4 Subdomain Best Practices

I am seeking some general insight and advise on this issue rather than a specific solution to a problem.

I am currently developing an application which will require an admin subdomain, for example: admin.mydomain.com and this admin subdomain will handle all things such as managing users and setting up content within the panel itself as well as API calls to Dropbox and what not.

Generally, the panel will crossover somewhat with existing models and controllers from the public side of the site as well as managing it's own content submitted by users who are administrators.

With that being said how should I go about designing this application?

Given that my stack consists of:

  • Nginx
  • Phusion Passenger
  • Ubuntu
  • Rails 4

I haven't the faintest idea whether or not 2 smaller applications are the go, because I don't know the best way to host 2 applications on the same server. Then there are things like larger memory requirements for two distinct processes, and routing issues between the two applications.

Conversely, if this was a single application I would need to set-up some funky routing to ensure that things like editing users are only available under the admin subdomain to logged in admins.

With all the content read online it gives me a reasonable understanding on how to setup some things 'physically' (all though I am still not sure in that area) but I am still not sure how to structure this, 2 apps or 1, best routing for a subdomain and so forth.

Any insight would be great, I am in quite the pickle.

If I were you, I would go with single application, because it has a lot of advantages:

  • Higher performance, because there are less memory consumed by single application, then by multiple.
  • Higher maintainability of the code.
  • Easier communication between 'user' and 'admin' part of the application.
  • Built-in routing mechanism of the rails application natively supports these kind of scenarios.
  • Easy configuration of a proxy server (like nginx) to support this.

If you are following my idea, I would use namespaces in routing to separate 'user' and 'admin' part. and use device gem to add authentication on admin part.

I have multiple application built with such architecture and it works awesome! :)

If you decide on two applications then you can explicitly set the admin.domain in a configuration file and point it one application then set the www. one to point to the other domain.

I did that on an application I wrote but that was a bit different to your structure. Could you not use a controller namespace as per rail guides? If not then Railscasts has some good info on subdomains.

http://railscasts.com/episodes/221-subdomains-in-rails-3 http://railscasts.com/episodes/123-subdomains-revised

Unless there is a reason not to, go with the simplest possible solution:

One application, including the normal part as well as the admin stuff. Like this you can access your models (or other application logic) from the admin panel as well, without having to find a way of sharing them across multiple apps. It's simply faster do build and maintain for the start. If the application grows too big you can still always split it later.

As for the how:

  1. Routing constraint on subdomain sending the stuff to the right controllers
  2. Separate controllers for all admin interactions (access control!), ideally name-spaced
  3. Share models or other business logic/objects as needed (no business-logic in controllers!)
  4. No change to anything else in the stack needed (all the logic in rails)

Be very careful when sharing anything but models (or other business logic objects) between the two. This does not only make the structure less "SOLID" but also will cause you problems when you decide to split it later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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