Search code examples
httptomcatiisportforwardingnetsh

How to share port 80 for applications running on different platforms on Windows 2008


I have an Apache Tomcat which is running on a Windows Server 2008 R2 Standard and hosting a web site (www.domain1.com). Apache is currently listening port 80 on the server. Now, on the same server I want to host a Http server application written in .NET (www.domain2.com) which will be running as a Windows service. I also want Http server to be accessed as www.domain2.com via port 80.

So simply I need is.

    Request                                   Forward To
1.  http://www.domain1.com/[anything]         http://localhost:8080/app1/[anything] <-- tomcat
2.  http://www.domain2.com/[anything]         http://localhost:8000/[anything] <-- windows service

I tried to use below netsh configuration but it did not help.

netsh interface portproxy add v4tov4 listenport=80 listenaddress=www.domain1.com connectport=8080 connectaddress=127.0.0.1 protocol=tcp
netsh interface portproxy add v4tov4 listenport=80 listenaddress=www.domain2.com connectport=8000 connectaddress=127.0.0.1 protocol=tcp

When I checked telnet localhost 80 from local machine or telnet www.domain1.com 80 from a remote machine it is not connecting.

I am open to any port forwarding solution that involve netsh, IIS, Tomcat or any other third party tool to achieve my goal.

SOLUTION

Following Carlos' recommendations below configuration worked

  1. Install UrlRewrite on IIS
  2. Install ARR on IIS
  3. IIS Home -> IIS -> Application Request Routing Cache -> Server Proxy Settings -> Check Enable Proxy
  4. Create a default web site listening port 80 with no hostname bound
  5. Add below to the web.config of the Default website

-

<system.webServer>
<rewrite>
<rules>
    <rule name="forward domain1 to tomcat">  
        <match url="(.*)" />  
        <conditions>
            <add input="{HTTP_HOST}" pattern="domain1.com" />
        </conditions>
        <action type="Rewrite" url="http://localhost:8080/app1/{R:1}" />  
    </rule>
    <rule name="forward domain2 to windows service">  
        <match url="(.*)" />  
        <conditions>
            <add input="{HTTP_HOST}" pattern="domain2.com" />
        </conditions>
        <action type="Rewrite" url="http://localhost:8000/{R:1}" />  
    </rule> 
</rules>
</rewrite>
</system.webServer>

Solution

  • A couple of recommnedations:

    1. Configure IIS in Port 80 to use a host name for port 80. The default web site installation sets up a binding for port 80 without any host name which means it will setup and http.sys registration to listen for all. If you just setup a host name there, then it will let you use that in other programs like .net.
    2. In your .net application (assuming you are using HttpListener which is built on top of http.sys), then you can make sure to use a binding (prefix) with a host name as well (it could even be localhost if needed), which will make it work and be nice to others listening in port 80 through http.sys.
    3. Finally, for your non-http.sys-nice-players like other http servers, etc, you can install Application Request Routing (http://www.iis.net/downloads/microsoft/application-request-routing) that will let you configure rules to proxy traffic to other ports or (even other machines if needed). so you can add rules in ARR to do that mapping and routing.