38

I want to specify host names with two different ports in the Windows hosts file.

Is there a way to do it? Or is it not allowed by Windows itself?

I have been wasting my time searching for the solution for the last 8 hours.

Is it possible to specify ports in the host file, hosts? E.g.: 127.0.0.1:80 and 127.0.0.1:9211

3 Answers3

27

Simply use IP addresses without ports. Example:

192.168.2.50  example.com

Then, to access 192.168.2.50:5555 from your browser (or other program):

http://example.com:5555/

The hosts file can be found at:

Linux /etc/hosts

Windows: C:\Windows\System32\drivers\etc\hosts

gdbdable
  • 666
19

You cannot associate a port number with a hostname mapped to an IP in the hosts file. You can achieve this with Fiddler though using FiddlerScript: 

if (oSession.HostnameIs("somesite.com")){
    oSession.bypassGateway = true;
    oSession["x-overrideHost"] = "1.2.3.4:8080";
}
19
  • The hosts file is for resolving hostnames to IP addresses only
  • If you do not specify a port as part of a URL, e.g. <protocol>://<hostname>[:<port>][/path], your browser will use the default port for the protocol: HTTP/80, HTTPS/443, FTP/21

Example 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 allow routing traffic to the right server when multiple servers share the same IP, applications typically allow you to modify their port if needed, but not their IP address.
    • "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' built-in networking tool netsh as a port proxy.


Overview

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

Actions

  1. Start your app's HTTP server on a custom port: localhost:8081
  2. Add a line in the hosts file that maps a free IP address to the app's hostname:
    • Example: 127.65.43.21 example.app
    • I suggested 127.65.43.21 but any free address in the subnet 127.0.0.0/8 can be used
    • Verify that 127.65.43.21:80 isn't already in use by another service. If it is, use a different IP. Check using: netstat -a -n -p TCP | FINDSTR "LISTENING"
  3. Add the following network configuration, using netsh:
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
  1. Try to 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 just to use v4tov4. Typically, these will be installed by default, otherwise, install them with 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.

Blindspots
  • 3,472