0

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

Jack
  • 139
  • 5

4 Answers4

1

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.

EEAA
  • 109,363
  • 18
  • 175
  • 245
1

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)

Dan Andreatta
  • 5,454
  • 2
  • 24
  • 14
0

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.

rmalayter
  • 3,762
  • 20
  • 28
-1

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

gorilla
  • 1,207
  • 9
  • 6
  • 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 01: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