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
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:
(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.
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: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)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.
What you are looking for is called 'Virtual Hosting'. Google it and you'll find lots of HOWTOs.