Rails app for multiple domains - ruby-on-rails

I need to create a service with the same five sites and one that will unite them, but everyone has to live on a separate domain. Maybe somehow run one instance Rails app redirect users depending on the domain? Or is it better to start on a new Rails app per instance?

Why not just set up one instance of the rails app, and configure your http server (Apache, Nginx or whatever) to listen to connections on all of those domains?

Genarally, there are two ways you can do that with one rails instance.
redirect the comming requests to diffrent uri path ('/site1', '/site2' etc) from the http server based on the domain names. I'm not a pro on setting up this. But i'm sure this is do-able.
redirect the comming requests to different path from the application controller in a before filter based on the value of request.url.host variable.
redirect_to my_site1_path if request.url.host == 'www.site1.com'
You can choose either of them, it's up to you :).

Related

Want to create staging subdomain e.g staging.example.com

I have e.g www.example.com as my website domain, and now as testing instance I want to add www.staging.example.com. How this can be achieved. I tried searching for solutions on google, but got confused. Any suggestions on this will be greatly appreciated.
Your first line of attack would be to create a new DNS record for the domain you want. This can usually be done from the control panel provided by the service/company you purchased the domain through.
If your staging server is hosted and available at the ip A.B.C.D for example, you'd create a new A record for www.staging that points to A.B.C.D
Once this is done, you'll want to add a new (v)host entry on your server that's housing the staging site (this differs whether you're using Apache, NGINX etc). A simple Google search on how to do this should suffice.

How do I serve two apps from different servers using the same domain

I have a Rails app hosted on Heroku which servers generated pages for marketing/seo purposes. I have other content pages on another server, which are static pages. I want to keep them both on the same domain, to build seo goodness on that domain.
example:
domain.com/blah-blah-blah-something
should really load a page from heroku-server-name/blah-blah-blah-something but it LOOKS like it's from domain.com.
Possible?
You can front the site with HAProxy and configure it for content switching. To do this you have the frontend listen on some internal IP address that's mapped to your external IP. The backends are your servers. You setup access control lists to determine which backend to send to. For example, this could be via some path name or file extension (such as .html goes to server A and .aspx goes to another). In the end, the user has no idea they are on two separate servers, cause they only see the site being served on one domain name.
Note:
1) You won't be able to share sessions (which I'm sure you're already aware of).
2) HAProxy doesn't handle https, so if you need https then you'll have to have something to handle https termination, such as nginx or haproxy or stunnel.
Hope this is applicable, because I'm not familiar with heroku.
The DNS record for the domain can only point to one IP address (and consequently server). You could configure your heroku based application to render content from another server by effectively fetching the site and displaying it.
I've borrowed this code snippet from this link:
def fetch_url(url)
r = Net::HTTP.get_response( URI.parse( url ) )
if r.is_a? Net::HTTPSuccess
r.body
else
nil
end
end
# use like this from your controller
#snippet = fetch_url "http://www.oreilly.com/"
# and in your page <%= #snippet %>
http://answers.oreilly.com/topic/1052-ruby-on-rails-how-does-one-render-html-from-another-web-server-to-a-ruby-on-rails-app/
There will likely be poor performance to any request like this as it will effectively take the time of two requests.

How do I serve multiple rails apps on one domain (and sub)?

This is kind of weird but I'd like to serve multiple websites on the same domain. If possible, we want to avoid subdomains to keep urls simple for our users - no need for them to know it's two separate apps. This is purely for keeping the code bases separate. Any ideas?
For example:
Rails App 1 (Refinery CMS) serves:
http://example.com/
http://example.com/about
http://example.com/pricing
Rails App 2 (our real App) serves:
http://example.com/account
http://example.com/store
http://example.com/listings
We use ruby 1.9.2, ruby on rails, refinery cms, apache and passenger.
If you're using Passenger, check out the Deploying to a sub URI portion of the manual - it's quite simple to set up an app on a sub-URI. You may need to set config.action_controller.relative_url_root in your app configuration, as well.
Edit: I misread the question; not one app per URI, but one app serving some (but not all) endpoints. This is actually moderately easy to do with some basic rewrites, as well.
Deploy your Rails app to, let's say, /railsapp (but without setting relative_url_root). Now, in .htaccess:
RewriteRule ^account/(.*)$ railsapp/account/$1 [L]
This will internally remap /account/* to /railsapp/account/*, so as long as you set up a rewrite per path your Rails app handles, it should work fine.
Subdomains make it easier (thus why most sites have shop.example.com), but you could probably use rewrite rules with name based virtual host routing. How exactly to do that I'm not sure. More of a Apache rewrite question for SuperUser.
A word of warning if you are using SSL you might have issues arise.
You could set it up to first hit one app where you expect most URLs would work and if it 404s you could instruct it to try the other app next, though this will be slower than routing per route but it will work without having to hardcode a route for every page that is created on say, Refinery CMS.
Currently I'm also working on a same kind of CMS. In my case also I need multiple sub domains, like
www.test1.mydomain.com
www.test2.mydomain.com
www.test3.mydomain.com
www.test4.mydomain.com
here is what I did
in rails 3 (if you are on rails3) you can get the sub domain by using request object. (If you are on rails 2.x you can use sub domain_fu plugin)
In my case I have used a before filter to get the sub domain, after that I load the site according to the sub domain
For development use the following public domain "lvh.me"
http://tbaggery.com/2010/03/04/smack-a-ho-st.html
this was very useful for me http://railscasts.com/episodes/221-subdomains-in-rails-3
let users have their domains forwarded to your subdomain (with masking)
ex : www.clientdomain.com --> http://client.mydomain.com
hope this helps
cheers
sameera

How to do URL rewrite in Rails?

I have a rails app that is accessible through multiple URLs, I was wondering what is the best way to rewrite the URL to use the main domain name, abc.com.
I have a bunch of other domain names like
1kjsdf.info
2lksjdfs.info
3sldkjfds.info
... in total 50 of these kinds of domains.
They all end in info if that makes it easier. I used lighttpd as my webserver, is there a way to set things up so that when the user goes to 1kjsdf.info\profile, the url is rewritten as adc.com\profile?
You should probably do this in lighttpd, not Rails.
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRewrite
It will be much faster to service these requests in the HTTP server before letting it get to Rails.

Account based lookup in ASP.NET

I'm looking at using ASP.NET for a new SaaS service, but for the love of me I can't seem to figure out how to do account lookups based on subdomains like most SaaS applications (e.g. 37Signals) do.
For example, if I offer yourname.mysite.com, then how would I use ASP.NET (MVC specifically) to extract the subdomain so I can load the right template (displaying your company's name and the like)? Can it be done with regular routing?
This seems to be a common thing in SaaS so there has to be an easy way to do it in ASP.NET; I know there are plugins that do it for other frameworks like Ruby on Rails.
This works for me:
//--------------------------------------------------------------------------------------------------------------------------
public string GetSubDomain()
{
string SubDomain = "";
if (Request.Url.HostNameType == UriHostNameType.Dns)
SubDomain = Regex.Replace(Request.Url.Host, "((.*)(\\..*){2})|(.*)", "$2");
if (SubDomain.Length == 0)
SubDomain = "www";
return SubDomain;
}
I'm assuming that you would like to handle multiple accounts within the same web application rather than building separate sites using the tools in IIS. In our work, we started out creating a new web site for each subdomain but have found that this approach doesn't scale well - especially when you release an update and then have to modify dozens of sites! Thus, I do recommend this approach rather than the server-oriented techniques suggested above based on several years worth of experience doing exactly what you propose.
The code above just makes sure that this is a fully formed URL (rather, say, than an IP address) and returns the subdomain. It has worked well for us in a fairly high-volume environment.
You should be able to pick this up from the ServerVariables collection, but first you need to configure IIS and DNS to work correctly. So you know 37Signals probably use Apache or another open source, unix web server. On Apache this is referred to as VirtualHosting.
To do this with IIS you would need to create a new DNS entry (create a CNAME yourname.mysite.com to application.mysite.com) for each domain that points to your application in IIS (application.mysite.com).
You then create a host header entry in the IIS application (application.mysite.com) that will accept the header yourname.mysite.com. Users will actually hit application.mysite,com but the address is the custom subdomain. You then access the ServerVariables collection to get the value to decide on how to customize the site.
Note: there are several alternative implementations you could follow depending on requirements.
Handle the host header processing at a hardware load balancer (more likely 37Signals do this, than rely on the web server), and create a custom HTTP header to pass to the web application.
Create a new web application and host header for each individual application. This is probably an inefficient implementation for a large number of users, but could offer better isolation and security for some people.
You need to configure your DNS to support wildcard subdomains. It can be done by adding an A record pointing to your IP address, like this:
* A 1.2.3.4
Once its done, whatever you type before your domain will be sent to your root domain, where you can get by splitting the HTTP_HOST server variable, like the user buggs said above:
string user = HttpContext.Request.ServerVariables["HTTP_HOST"].Split(".")
//use the user variable to query the database for specific data
PS. If you are using a shared hosting you're probably going to have to by a Unique IP addon from them, since it's mandatory for the wildcard domains to work. If you're using a dedicated hosting you already have your own IP.
The way I have done it is with HttpContext.Request.ServerVariables["HTTP_HOST"].Split(".").
Let me know if you need more help.

Resources