How to Configure Windows Hosts File with IP:Port Mapping for Web Development


2 views

The Windows hosts file (located at %SystemRoot%\System32\drivers\etc\hosts) is a simple text file that maps hostnames to IP addresses. However, it's important to understand that the hosts file specification (RFC 952) doesn't support port numbers in the mapping entries.

When you try entries like:

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

The system will ignore everything after the IP address, including the port specification, because the hosts file wasn't designed to handle port mappings.

For developers needing to route traffic to different ports on localhost, here are effective solutions:

1. Using Different Hostnames

Configure your hosts file with distinct hostnames and set up your web servers to respond to these:

127.0.0.1    site1.local
127.0.0.1    site2.local

Then configure your web servers:

  • Apache (httpd-vhosts.conf):
    <VirtualHost *:80>
        ServerName site1.local
        DocumentRoot "C:/websites/site1"
    </VirtualHost>
    
    <VirtualHost *:8080>
        ServerName site2.local
        DocumentRoot "C:/websites/site2"
    </VirtualHost>

2. Reverse Proxy Solution

Set up Nginx as a reverse proxy:

server {
    listen 80;
    server_name site1.local;
    
    location / {
        proxy_pass http://127.0.0.1:80;
    }
}

server {
    listen 80;
    server_name site2.local;
    
    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

For advanced routing needs, you can use Fiddler's scripting capability:

if (oSession.HostnameIs("www.site1.com")) {
    oSession.bypassGateway = true;
    oSession["x-overrideHost"] = "127.0.0.1:80";
}
else if (oSession.HostnameIs("www.site2.com")) {
    oSession.bypassGateway = true;
    oSession["x-overrideHost"] = "127.0.0.1:8080";
}

For frequent changes, create a PowerShell script to manage your hosts file:

function Add-HostEntry {
    param(
        [string]$ip,
        [string]$hostname
    )
    
    $hostsPath = "$env:windir\System32\drivers\etc\hosts"
    $entry = "n$ipt$hostname"
    
    if (-not (Select-String -Path $hostsPath -Pattern $hostname -Quiet)) {
        Add-Content -Path $hostsPath -Value $entry
        Write-Host "Added: $ip → $hostname"
    } else {
        Write-Host "Entry exists for $hostname"
    }
}

Add-HostEntry -ip "127.0.0.1" -hostname "site1.local"
Add-HostEntry -ip "127.0.0.1" -hostname "site2.local"

The Windows hosts file (located at %SystemRoot%\System32\drivers\etc\hosts) doesn't support port specifications in its entries. This is a fundamental design limitation of how hostname resolution works at the OS level.

When you try entries like:

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

The system simply treats :80 and :8080 as part of the hostname, not as port indicators. DNS resolution happens before port specification in the network stack.

Here are effective alternatives for your Apache/IIS setup:

Option 1: Use Different IP Addresses

127.0.0.1       www.site1.com
127.0.0.2       www.sitetwo.com

Then configure your servers:

  • IIS: Bind to 127.0.0.1:80
  • Apache: Bind to 127.0.0.2:8080

Option 2: Virtual Hosts Configuration

For Apache (httpd-vhosts.conf):

<VirtualHost *:8080>
    ServerName www.sitetwo.com
    DocumentRoot "C:/path/to/your/site"
</VirtualHost>

Option 3: Reverse Proxy Setup

Configure Apache to proxy requests to IIS:

<VirtualHost *:80>
    ServerName www.site1.com
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

For complex setups, consider running a local DNS server like dnsmasq:

# Example dnsmasq configuration
address=/www.site1.com/127.0.0.1
address=/www.sitetwo.com/127.0.0.1
port=53

Some browser extensions allow overriding host-port combinations:

  • Chrome: "Host Switch Plus" extension
  • Firefox: "HostAdmin" add-on

While the hosts file approach would be convenient, understanding these workarounds gives you more control over your local development environment. For most developers, using different IP addresses or virtual hosts provides the cleanest solution.