15

I want to configure the Windows hosts file to send host requests based on IP address + ports.

For example:

127.0.0.1:80       www.site1.com
127.0.0.1:8080     www.sitetwo.com

Because I have Apache listening on port 8080 and IIS on port 80 (default).

So, I think the best way to do this is to modify the hosts file. It's not affecting anything. Neither I am getting any error nor any message.

2

8 Answers 8

11

You can't. The hosts file is just that -- hosts. It's the equivalent of a "short-circuited" DNS within your local machine. DNS can't send you to ports, either.

To achieve what you're trying to do, you would want to set up virtual hosting under IIS or Apache, and use that (based on the HTTP Host: header) to let the service decide which site to present. This won't help you, though, if you want to be doing this with both IIS and Apache simultaneously.

1
  • could you be please clear, i didnt understand about virtual hosting. How can i do the virtual hosting?
    – SIA
    Feb 21, 2010 at 18:43
6
  • The hosts file is for host name resolution only
  • The browser, in the absence of directly specifying the port: i.e. <hostname>:<port>, defaults to port 80

###Typical Problem Scenario###

  1. applications typically set their servers to the same default ip address 127.0.0.1 aka localhost (defined in the hosts file).
  2. to avoid collision between possibly other existing/running servers, the application typically allows you to change the port, but not the ip address.

2a. If you could change the servers ip address to another in the loopback reserved address space 127.0.0.0/8, then you probably wouldn't be attempting to set ports in the hosts file.

Possible solution

You can work around this using Windows included Networking tool netsh as a port proxy.


Overview

example.app
 |                               <--browser defaults to port 80
 +--> example.app:80
       |                         <--Hostname resolution by Hosts File
       +--> 127.65.43.21:80      
             |                   <--Link by netsh Utility
             +--> 127.0.0.1:8081

Actions

  • Start your server on localhost:8081
  • Add the "local DNS" in the hosts file as a new line
    • 127.65.43.21 example.app
      • Any free address in the network 127.0.0.0/8 can be used.
      • Note: I am assuming 127.65.43.21:80 is not occupied by another service.
      • You can check with netstat -a -n -p TCP | grep "LISTENING"
  • add the following network configuration with netsh command utility
    • netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
  • Access the server at http://example.app

*Notes:

  • These commands/file modifications need to be executed with Admin rights*

- netsh portproxy needs ipv6 libraries even only to use v4tov4, typically they will also be included by default, otherwise install them using the following command: netsh interface ipv6 install


You can see the entry you have added with the command:

netsh interface portproxy show v4tov4

You can remove the entry with the following command:

netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.65.43.21


###Links to Resources:###

Note: this answer is a duplication of my answer discussed in this similar question/answer on stackoverflow.

1
4

You can do that with Fiddler. With Fiddler script, you can many amazing things.

If you set hosts like this

127.0.0.1     www.site1.com    # Port 80
127.0.0.1     www.sitetwo.com  # Port 8080

Add this in CustomRules.js(to open CustomRules.js, choose Customize Rules on Fiddler's Rules menu)

// this method is already exist
static function OnBeforeRequest(oSession: Session) {  
    if (oSession.host.toLowerCase() == "www.sitetwo.com") 
        oSession.host = "www.sitetwo.com:8080";
    ...
}

Then Fiddler convert the host, and you'll connect to port 8080.

1
  • Are you aware of any open source alternative (james, owasp zap, or other) which can be used to the same effect?
    – Marc.2377
    Dec 21, 2018 at 18:36
3

No, you can't. If it's not working, then something else is going wrong.

127.0.0.1 site.com
127.0.0.1 www.site.com

I assume this is for a development box?

DNS will resolve site.com and www.site.com to localhost as long as you're not doing something like proxying your DNS queries. You should probably ask over at ServerFault for webserver configuration help, that's the most likely issue.

2
  • Thanks for the help!!! But there is no way??? Then how can i run iis and apache parallelly with different ports
    – SIA
    Feb 21, 2010 at 18:41
  • They're still going to the same address, the port doesn't even factor into what hosts does. The port will still work, you can run them in parallel.
    – Xorlev
    Feb 21, 2010 at 19:05
1

Are you using the servers only for local development? If so, you could put your Apache to listen to 127.0.0.2:80 and redirect that with the hosts file to the domain name you like.

0

As with the other answers host file doesn't have port information but you can have a page that redirects biased upon the header like apache or IIS would do for name based virtual hosting. Check the header and redirect as needed. However I don't really see the value in this as you are reinventing the wheel. Apache supports port based virtual hosting and I assume that iis does.

Is your goal to have a development machine with both running our is this for a production situation.

0

Follow these steps:

Install Fiddler2

Navigate to Fiddler2 menu:- Tools > HOSTS.. (Click to select)

Add a line like this:-

localhost:8080        www.mydomainname.com

Save the file & then checkout www.mydomainname.com in browser.

https://stackoverflow.com/questions/8652948/using-port-number-on-windows-xp-host-file

-1

This is what you can do, it would be helpful for local development only.

hosts file:

  • 127.0.0.1 local.site1.com
  • 127.0.0.1 local.site2.com

In IIS, create new website and for local.site1.com & local.site2.com

IIS Setup

  • IP Address : All Unassigned (it would use local host or any other available ip)
  • Port : any other than already in use (81,82,83 and so on. 80 is already in use by Default Website)
  • Host Name : Leave it blank

You are good to go. Just make sure ports you have used are not in use by some other process & are also open from firewall, antivirus etc.

After this you can use following urls for accessing websites from local machine or from LAN.

You must log in to answer this question.