Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a webserver tha actually has a tomcat6 server on port 80 and that is reachable from domain1.com. I would like to open another website on same machine using a sinatra (ruby) server and be able to reach it from domain2.com.

What should I do to achieve this? I suppose first move tomcat6 from 80 to another port and then place something local on 80 that tunnels to different webserver by using which domain the request came from.

But I really don't know how..

thanks in advance

share|improve this question

4 Answers 4

up vote 1 down vote accepted

Yes, this is quite easy to do with apache's mod_proxy. As you suggested, I'd move tomcat to port 8080 or something and then have apache listen on 80. Likewise, say you set sinatra to listen on port 8888. Then in apache, you do something like this:

<Virtualhost *:80>
    ServerName domain1.com

    # Proxy through to tomcat, listening on port 8080
    <Location />
                ProxyPass http://localhost:8080/
                ProxyPassReverse http://localhost:8080/

                Order allow,deny
                Allow from all
    </Location>
</VirtualHost>

<VirtualHost *:80>
    ServerName domain2.com

    # Proxy through to sintra, listening on port 8888
    <Location />
                ProxyPass http://localhost:8888/
                ProxyPassReverse http://localhost:8888/

                Order allow,deny
                Allow from all
    </Location>
</VirtualHost>

(make sure that mod_proxy is installed and enabled)

When that's complete, you should be able to enter domain1.com (assuming that you already have DNS record for that pointed at your server) in your browser and apache will proxy through to tomcat listening on 8080. Likewise, domain2.com will be proxied through to sinatra.

I'm sure that this could be done easily with nginx, haproxy, or something else quite easily. I don't have experience with those, though, so you'll have to look elsewhere if you want to go that direction.

share|improve this answer

You can also use iptables to get a result similar to using mod_proxy (as ErikA is showing you). Basically you redirect the packet to a different port based on source IP, something like:

sudo iptables -t nat -A PREROUTING -p tcp -s domain1.com --dport 80 -j REDIRECT --to-ports 8080
sudo iptables -t nat -A PREROUTING -p tcp -s domain2.com --dport 80 -j REDIRECT --to-ports 8081

Here tomcat listens on port 8080 and sinatra listens on port 8081.

(you may want to use something like Shorewall or some other firewall management tool instead of manipulating iptables directly)

share|improve this answer
    
This seems a clever approach.. I'll do some tests! I would like to upvote you but not enough rep yet :( –  Jack Sep 22 '10 at 11:41
    
now I can do it.. here you are :) –  Jack Sep 22 '10 at 14:54

Only one process can typically own a port, so you'll need some form of HTTP proxy to actually listen on port 80, which has the smarts to forward requests to Tomcat or sinatra based on host name (or whatever other criteria, like URL path). While Apache mod_proxy can do this, it has some limitations, and Apache can be heavyweight. I would suggest a purpose-built proxy like nginx or lighthttpd. Much smaller footprint in terms of memory, and faster under high load if that is your need.

share|improve this answer

What you are looking for is called 'Virtual Hosting'. Google it and you'll find lots of HOWTOs.

share|improve this answer
    
It's more than just "Virtual Hosting". His request would require a proxy as well, which your answer does not take into consideration. –  EEAA Sep 21 '10 at 1:05
    
Yes it does. The first hit for virtual hosting and tomcat has the exact setup you need, including an apache proxy. It's the teaching a guy to fish senario. –  gorilla Sep 21 '10 at 23:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.