How can I reroute a sub-domain to localhost + port number?

I have several web applications running on my developer machine. They mimic our production web applications which are hosted on sub-domain. For example, consider:

api.myserver.com - is mimicked by 127.0.0.1:8000     
www.myserver.com - is mimicked by 127.0.0.1:8008    
and so on...

How can I make it so that, on my Windows 7 machine, HTTP calls to "api.myserver.com" (note the lack of port number) are redirected to 127.0.0.1:8000 etc? Note that this needs to apply both to client-side calls (in the browser) and server-side calls (from IIS to Python development server and vice versa).

Do I need a proxy to run locally to achieve this? Can you recommend such a tool?


Solution 1:

Actually the solution to your problem is much simpler than port translation.

Since the entire 127.x.x.x IP block is routed to the local host you can use any IP you want in this block and bind it to each web site.

For examlpe, edit your hosts file to bind:

127.0.0.1    www.site.com
127.0.0.2    api.site.com
127.0.0.3    dev.site.com

Configure each site to bind to its IP address.

Note that since you want to use other servers beside IIS, and IIS tends to hoard all the available IPs for port 80 you need to disable socket pooling for this to work perfectly:

http://www.iislogs.com/steveschofield/iis7-post-44-iis7-and-apache-on-the-same-machine

Solution 2:

Apache with mod_proxy does what you want. Example:

<VirtualHost 127.0.0.1:80>
    ServerName api.myserver.com
    ProxyRequests On
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8000/
</VirtualHost>

Define a VirtualHost definition like this for every subdomain you need.