# Multiple virtual host?

**URL:** <https://www.ruby-forum.com/t/multiple-virtual-host/90874>\
**Category:** Rails\
**Created:** [April 2, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874 "2007-04-02T17:39:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 2, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/1 "2007-04-02T17:39:19Z")

</div>

I am trying to serve up two applications from one box. (It’s not a  
commercial system; it’s just a box on my desktop, that I’m using to test  
out RoR.)

SETUP. In my httpd.conf, I’ve got something like

\<VirtualHost \*:80\>  
ServerName URL  
ServerAlias URL  
ServerAdmin USER@PLACE  
ProxyPass /APP [http://127.0.0.1:8000/APP](http://127.0.0.1:8000/APP)  
ProxyPassReverse /APP [http://127.0.0.1:8000/APP](http://127.0.0.1:8000/APP)  
ProxyPass /images [http://127.0.0.1:8000/APP/images](http://127.0.0.1:8000/APP/images)  
  
\<VirtualHost \*:80\>  
ServerName URL  
ServerAlias URL  
ServerAdmin USER@PLACE  
ProxyPass /NEWAPP [http://127.0.0.1:8010/NEWAPP](http://127.0.0.1:8010/NEWAPP)  
ProxyPassReverse /NEWAPP [http://127.0.0.1:8010/NEWAPP](http://127.0.0.1:8010/NEWAPP)  
ProxyPass /images [http://127.0.0.1:8010/NEWAPP/images](http://127.0.0.1:8010/NEWAPP/images)

where URL is for my machine, and APP and NEWAPP are the names of two  
applications, which I serve up with

mongrel\_rails start -d -p 8000 -a 127.0.0.1 -e development --prefix /APP  
mongrel\_rails start -d -p 8010 -a 127.0.0.1 -e development --prefix  
/NEWAPP

THE PROBLEM. Only one site works. (The problem is in the setup, not  
the site code, because I can make _either_ one work by commenting out  
parts of my httpd.conf file.)

Q: how should I set up httpd.conf (or mongrel\_rails) to serve two  
applications from one box?

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 2, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/2 "2007-04-02T20:56:11Z")

</div>

The ServerName (and ServerAlias) need to be different for the two  
different virtual hosts. It’s not enough to just proxy them to different  
internal URLs.

b

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 1:26am UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/3 "2007-04-03T01:26:19Z")

</div>

Interesting. But the URL (“[my.computer.com](http://my.computer.com)”, in the examples below) is  
determined by my system adminstrator and someone who set up a DNS table,  
isn’t it? I’m confused; the examples I’ve seen suggest that I can just  
invent new URLs, but I don’t see how that can work.

Or is the idea to put something after the “/” in a URL, e.g. to name the  
two sites

[my.computer.com/a/APP](http://my.computer.com/a/APP)

for APP, and

[my.computer.com/b/NEWAPP](http://my.computer.com/b/NEWAPP)

for NEWAPP?

(And, should I therefore be calling mongrel\_rails differently?)

I’m sorry I’m being so dense on this. I’m only following up, continuing  
to be a pest, because I think others might be interested.

D

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 2:03am UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/4 "2007-04-03T02:03:09Z")

</div>

Given this, I wonder how one can host more than one RoR site on a given  
box, without changing any DNS tables on a higher-up box.

At least from my limited academic perspective, this is an important  
facet of a web scheme. I have many little websites on my box, e.g. one  
for each course I teach, one for each multi-author paper I write, one  
for each multi-PI proposal I write, etc. (I wonder whether the web  
would have spread, in its early academic days, if things weren’t so  
easy.)

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 3:03am UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/5 "2007-04-03T03:03:45Z")

</div>

If you don’t want to get another domain name, you may ask other people  
to update their hosts file (/etc/hosts), or you may serve each websites  
on different port.

The idea of virtual host is that requests are forwarded according to the  
domain name of the request. So that [www.abc.com](http://www.abc.com) and [www.def.com](http://www.def.com) will be  
served differently even though the ip address is the same.

Alternatively, you may obtain multiple IP addresses and bind each site  
to it if you are only serving in local area network.

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/6 "2007-04-03T15:00:11Z")

</div>

My summary so far, for anyone catching this thread with a search:

PHP: users with no special privileges can create dozens of sites using  
the (HOST)/~(USER)/(SITENAME) scheme. This is done without restarting  
the web server, and without editing (and thus potentially damaging)  
configuration files.

RoR: some special magic is required. (I’m not sure what, yet.)

Thus PHP is analagous to HTML, in the sense that ease of use is likely  
to create a wide community of experts. I hope that RoR (and Django and  
the like) can build up such a community somehow.

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/7 "2007-04-03T15:22:39Z")

</div>

Hi Dan,

I believe you have got the wrong idea about virtual host, virtual host  
is for domain name mapping, not subdirectory. You can do this in rail  
with by updating the route.rb to override the default routing rule, or  
with restful. I believe restful is the way to go for the  
(HOST)/~(USER)/(SITE) scenario.

You can also create a controller for each user if each user request is  
handled differently.

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 1:51am UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/8 "2007-04-03T01:51:02Z")

</div>

You can’t just make up URLs and expect them to work on the open web…  
that’s what DNS is for.

For using strictly on your local machine, you can make up host names and  
put them in your hosts file (/etc/hosts on \*nix, google for it on  
windows).

b

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/9 "2007-04-03T21:21:59Z")

</div>

I don’t think this has anything to do with Rails. You would have the  
same problem if you were using PHP. This boils down to a mis-  
understanding of the purpose of the VirtualHost directive in Apache.  
The way virtual hosts work is to match against the Host header in the  
HTTP request (basically the client connects to the server using the  
server’s IP but also provides the hostname it was initially asked to  
connect to). So if i set up my DNS zones so that [www.blah.com](http://www.blah.com) and  
[www2.blah.com](http://www2.blah.com) both resolve to the same IP my browser will provide  
which of the 2 I asked for to the server when connecting. Apache then  
uses this information to determine which VirtualHost directive to use  
to process the request. The path that is being requested (the /  
blah.html in [www.blah.com/blah.html](http://www.blah.com/blah.html)) is part of the actual request,  
not the HTTP headers and thus not available to Apache at the time when  
it needs to make its vhost decision. If you want to do path based  
determination of settings you need to use a directive  
_within_ your VirtualHost directive that set up the proxy settings you  
wish to use for each location. Have a look at  
[core - Apache HTTP Server Version 2.2](http://httpd.apache.org/docs/2.2/mod/core.html#location) for more  
information (assuming Apache 2.2…similar sections are available for  
other version of the Apache server/documentation).

Best of luck!

~Ross

On Apr 3, 9:00 am, Dan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/10 "2007-04-03T22:48:16Z")

</div>

The only reason “(HOST)/~(USER)/(SITENAME)” works for you is because the  
sysadmin of your server configured apache to map ~username to a  
directory in your home directory. The php then works in this scenario  
because it is just template files, html with php code which mod\_php runs  
and replaces.

Rails does not work on this principle. It is for building complete,  
well-architected web applications. The template files (the html with  
ruby embedded) are not publicly accessible because Rails processes the  
file for us and writes the final output into the http socket output  
stream.

That probably sounds overly academic, but there are very good reasons  
why it is designed that way. Basically, if you just want to embed some  
code into an html file, stick with php.

b

PS: Oh, with correct configuration of apache, you can use the ~user  
stuff with rails as well. However, you’d need edit access to the apache  
config and you’d need to know what you are doing.

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 3, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/multiple-virtual-host/90874/11 "2007-04-03T23:40:03Z")

</div>

> If you want to do path based  
> determination of settings you need to use a directive  
> _within_ your VirtualHost directive

Thanks, Ross, I’ll check into that.

I’ve been trying to deploy my app for months now. (A search on this  
forum would illustrate the kindness of strangers who have helped me in  
this, and my very slow understanding of the advice offered.) Luckily,  
my app is mainly an experiment to see whether RoR will prove useful for  
me, so I have the luxury of revisiting the problem in the evenings or on  
weekends.

I’ve found that the ratio of (effort in learning RoR) to (effort in  
deploying RoR) to be unexpectedly high. It’s a bit of a shame that this  
ratio is so seldom mentioned in the tutorials that (reasonably) sing the  
praises of RoR.

Oh well, I enjoyed reading the Agile book on RoR, so I guess I’ll buy  
the book on deploying RoR as well … even though, in the end, I expect  
I’ll just be copying ten lines from the book into my httpd.conf file.
