How to Test a Server’s Domain Configuration Before DNS Propagation Using /etc/hosts and Raw HTTP


2 views

When migrating a website from Server A to Server B, you need to verify the new server's configuration is correct before modifying DNS records. The core problem is: how to request www.example.com from Server B when DNS still points to Server A.

The most straightforward method is modifying your local machine's hosts file:

# Linux/macOS: /etc/hosts
# Windows: C:\Windows\System32\drivers\etc\hosts

192.0.2.45 www.example.com  # Replace with Server B's IP

After saving the file, flush your DNS cache:

# Linux/macOS
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder

# Windows
ipconfig /flushdns

When you can't modify the hosts file (e.g., on restricted systems), use telnet or netcat to send raw HTTP requests:

telnet 192.0.2.45 80  # Connect to Server B's IP
GET / HTTP/1.1
Host: www.example.com
Connection: close

# Or with curl:
curl --resolve www.example.com:80:192.0.2.45 http://www.example.com

For comprehensive testing, cURL offers several useful options:

# Test HTTPS (note: certificate will verify against original domain)
curl -vk --resolve www.example.com:443:192.0.2.45 https://www.example.com

# Test specific headers
curl -I --resolve www.example.com:80:192.0.2.45 http://www.example.com

# Test POST requests
curl -X POST --resolve www.example.com:80:192.0.2.45 \
  -d "test=data" http://www.example.com/api

For frequent migrations, create a test script:

#!/bin/bash
SERVER_B_IP="192.0.2.45"
DOMAIN="www.example.com"

echo "Testing HTTP..."
curl -sS -o /dev/null -w "%{http_code}" \
  --resolve "$DOMAIN:80:$SERVER_B_IP" \
  "http://$DOMAIN"

echo -e "\nTesting HTTPS..."
curl -sS -o /dev/null -w "%{http_code} %{ssl_verify_result}" \
  --resolve "$DOMAIN:443:$SERVER_B_IP" \
  "https://$DOMAIN"
  • HTTPS certificates will validate against the original domain name
  • Session-dependent functionality may require cookie handling
  • Test all critical application paths and API endpoints
  • Verify database connections and backend services

Other useful tools for this scenario:

  • Postman: Configure the hosts file then use normally
  • Browser extensions: ModHeader (Chrome) or similar to override Host header
  • Docker: Spin up temporary containers with custom DNS settings

When migrating web services between servers, the most straightforward method is modifying the local hosts file. This allows you to override DNS resolution temporarily:

# Linux/MacOS /etc/hosts entry
192.168.1.100 www.example.com

# Windows hosts file location
C:\Windows\System32\drivers\etc\hosts

When you can't modify system files, use curl with custom headers:

curl -H "Host: www.example.com" http://server-b-ip/

For situations requiring low-level testing, telnet works as a manual HTTP client:

telnet server-b-ip 80
GET / HTTP/1.1
Host: www.example.com
Connection: close

# (press enter twice after the last header)

Modern browsers offer developer tools for forced host resolution:

  • Chrome/Edge: Use --host-resolver-rules flag
  • Firefox: about:config → network.dns.forceResolve

Here's a Python verification script using requests:

import requests

def test_server(ip, domain):
    headers = {'Host': domain}
    try:
        response = requests.get(f"http://{ip}", headers=headers)
        return f"Status: {response.status_code}, Content-Length: {len(response.content)}"
    except Exception as e:
        return f"Connection failed: {str(e)}"

print(test_server("192.168.1.100", "www.example.com"))

Remember to flush DNS cache after testing:

# Windows
ipconfig /flushdns

# Linux (systemd-resolved)
systemd-resolve --flush-caches

# MacOS
dscacheutil -flushcache