
ERR_CONNECTION_REFUSED on Hosts File Domains: 6 Fixes (2026)
Custom domain in your hosts file returning ERR_CONNECTION_REFUSED? Fix port mismatches, 0.0.0.0 vs 127.0.0.1 binding, Docker mappings and reverse proxies.
Table of Contents
- Find your situation: Select your role and stack
- Why this error happens (in 30 seconds)
- Fix 1: Add the missing port number to your URL (Frontend developers)
- Fix 2: Fix Localhost and 0.0.0.0 binding conflicts (Node.js, Vite, Next.js)
- Solution A: Bind your dev server to 0.0.0.0 (Recommended)
- Solution B: Add dual-stack IPv4 and IPv6 entries to your hosts file
- Fix 3: Expose container ports in Docker and Docker Compose (DevOps & Backend)
- Docker Compose configuration
- Docker CLI command
- Fix 4: Set up a local reverse proxy for clean URLs (Caddy or Nginx)
- Caddy setup (Fastest approach)
- Fix 5: Inspect active socket listeners (Sysadmins & Debugging)
- On macOS and Linux
- On Windows (PowerShell as Administrator)
- Fix 6: Switch from .dev to .test TLD (QA & Local domain testing)
- Advanced troubleshooting and rare edge cases
- 1. Flush browser socket pools (Chrome and Edge)
- 2. Verify local firewall permissions
- Manage local domains cleanly with Sleezr
ERR_CONNECTION_REFUSED on a hosts file domain means your computer successfully resolved the domain name to an IP address, but no service is listening on the requested port.
Your hosts file did its job: it mapped the domain to an IP (usually 127.0.0.1). The rejection happens at the network layer because your browser expects a server on port 80 (HTTP) or port 443 (HTTPS), while your development process is running on a custom port (like 3000 or 5173), bound to an incompatible interface, or stopped.
Find your situation: Select your role and stack
Scan the matrix below to identify your role, stack, and exact solution:
| Your Role / Status | Your Stack / Setup | Root Cause | Immediate Action |
|---|---|---|---|
| Frontend Developer | Vite, Next.js, React, Astro | Port 3000/5173 omitted in URL | See Fix 1: Add the port to your browser URL |
| Frontend Developer | Vite on localhost vs custom domain | Dev server bound to IPv6 (::1) | See Fix 2: Bind server to 0.0.0.0 |
| Backend / Full-Stack | Express, Fastify, NestJS, Go, Python | App listening on 127.0.0.1 only | See Fix 2: Fix 0.0.0.0 interface binding |
| DevOps / Platform | Docker, Docker Compose, Podman | Container port not published to host | See Fix 3: Expose container ports in Compose |
| Full-Stack / Team Lead | Multi-app local development | Want clean URLs without :3000 | See Fix 4: Set up a local Caddy reverse proxy |
| QA / Tester | Custom .dev or .app local domains | Browser forces HTTPS via HSTS | See Fix 6: Switch from .dev to .test TLD |
| Sysadmin / Power User | Port conflicts, firewall, zombie process | Process dead or port held by other app | See Fix 5: Inspect active socket listeners |
Why this error happens (in 30 seconds)
The hosts file has a single responsibility: mapping a hostname to an IP address. It cannot configure ports, protocols, or URL paths.
# Valid entry in /etc/hosts:
127.0.0.1 api.local.test
# Invalid syntaxes (ignored or syntax error):
127.0.0.1:3000 api.local.test
http://127.0.0.1 api.local.testWhen you visit http://api.local.test in your browser:
api.local.test to 127.0.0.1.See the hosts file syntax and format guide for formatting rules.
---
Fix 1: Add the missing port number to your URL (Frontend developers)
If your dev server runs on a dedicated port (3000 for Next.js, 5173 for Vite, 8080 for Webpack), your browser will not guess it automatically. You must type the port in the address bar.
- Fails:
http://api.local.test(connects to port 80) - Works:
http://api.local.test:3000(Next.js, Remix, Express) - Works:
http://api.local.test:5173(Vite, SvelteKit, Astro) - Works:
http://api.local.test:8080(Webpack, Vue CLI, Spring Boot)
If you want clean domain URLs without typing port numbers every time, jump to Fix 4 to set up a local reverse proxy.
---
Fix 2: Fix Localhost and 0.0.0.0 binding conflicts (Node.js, Vite, Next.js)
Modern frameworks frequently bind to localhost, which operating systems resolve to IPv6 loopback (::1). If your hosts file only contains an IPv4 line:
127.0.0.1 api.local.testAny browser request to api.local.test sends IPv4 packets to 127.0.0.1. If your dev server only listens on IPv6, the connection is instantly refused.
Solution A: Bind your dev server to 0.0.0.0 (Recommended)
Configuring your server to listen on 0.0.0.0 accepts incoming connections from all local IPv4 network interfaces:
- Vite CLI:
vite --host 0.0.0.0 - Vite Config (
vite.config.ts):
``typescript export default { server: { host: '0.0.0.0', port: 5173, }, }; ``
- Next.js:
next dev -H 0.0.0.0 - Node.js / Express:
``javascript const PORT = process.env.PORT || 3000; app.listen(PORT, '0.0.0.0', () => { console.log(Server running on port ${PORT}); }); ``
Solution B: Add dual-stack IPv4 and IPv6 entries to your hosts file
Add both loopback records to ensure resolution succeeds regardless of whether the browser requests IPv4 or IPv6:
127.0.0.1 api.local.test
::1 api.local.testRead 0.0.0.0 vs 127.0.0.1 and what is localhost for full details.
---
Fix 3: Expose container ports in Docker and Docker Compose (DevOps & Backend)
Docker containers run in isolated network namespaces. Mapping 127.0.0.1 myapp.test in your host hosts file directs traffic to your host machine, not inside the container. You must publish the container port to the host.
Docker Compose configuration
Add explicit port bindings under the ports key in docker-compose.yml:
services:
web:
image: nginx:alpine
ports:
- "80:80" # Host port 80 -> Container port 80
- "443:443" # Host port 443 -> Container port 443
- "3000:3000" # Host port 3000 -> Container port 3000Docker CLI command
If you start containers with docker run, include the -p flag:
docker run -d -p 80:80 -p 3000:3000 --name my-app my-app-imageSee also Docker hosts file management.
---
Fix 4: Set up a local reverse proxy for clean URLs (Caddy or Nginx)
If your goal is to access http://api.local.test or http://dashboard.local.test directly on port 80 without port numbers, run a lightweight local reverse proxy.
Caddy setup (Fastest approach)
Caddyfile in your project:```text api.local.test { reverse_proxy 127.0.0.1:3000 }
dashboard.local.test { reverse_proxy 127.0.0.1:5173 } ```
``bash caddy run ``
Caddy listens on ports 80 and 443, routing requests to each respective development server.
---
Fix 5: Inspect active socket listeners (Sysadmins & Debugging)
Verify that your application process is active and bound to the expected port before troubleshooting browser settings.
On macOS and Linux
# Check standard HTTP port 80
sudo lsof -nP -iTCP:80 -sTCP:LISTEN
# Check custom application port (e.g., 3000)
sudo lsof -nP -iTCP:3000 -sTCP:LISTEN- No output returned: The dev server is stopped, failed to compile, or crashed. Check your IDE terminal.
- Different application name returned: Another process occupies the port. Stop that process or assign a different port to your project.
On Windows (PowerShell as Administrator)
Get-NetTCPConnection -LocalPort 80,3000 -State Listen | Select-Object LocalAddress, LocalPort, OwningProcess---
Fix 6: Switch from .dev to .test TLD (QA & Local domain testing)
Top-level domains like .dev, .app, and .page are owned by Google and included in the browser HSTS preload list. Chrome and Firefox force HTTPS connections to these domains even on localhost.
If your local server does not have TLS certificates configured on port 443, navigating to myapp.dev results in an immediate ERR_CONNECTION_REFUSED.
- Avoid:
myapp.dev,project.app,api.page - Use instead:
myapp.test,project.localhost,api.local.test
The .test and .localhost TLDs are reserved by RFC 2606 specifically for testing and will never trigger mandatory HTTPS.
Read why .test is the best TLD for local development and how to configure local SSL certificates.
---
Advanced troubleshooting and rare edge cases
If your ports, bindings, and Docker configs are verified but ERR_CONNECTION_REFUSED persists:
1. Flush browser socket pools (Chrome and Edge)
Chromium browsers cache failed socket connection states. Even after starting your server, Chrome may keep rejecting connections until the socket pool is cleared.
chrome://net-internals/#socketschrome://net-internals/#dns and click Clear host cacheCmd+Shift+R on Mac, Ctrl+F5 on Windows)2. Verify local firewall permissions
Security software may silently drop inbound connections on non-standard ports:
- macOS: Check System Settings > Network > Firewall > Options to ensure your terminal or Node binary is allowed.
- Windows: Check Windows Defender Firewall with Advanced Security for inbound rules blocking Node.js or Docker.
---
Manage local domains cleanly with Sleezr
Managing hosts file entries across multiple stacks, microservices, and staging domains manually is prone to port confusion and syntax mistakes.
Sleezr gives developers and QA teams a visual dashboard to organize domains into toggleable project environments, verify IP bindings, and automatically flush DNS caches without touching the terminal.
Frequently Asked Questions
DNS resolution succeeded, but no server is listening on the requested port (port 80 for HTTP or 443 for HTTPS) at the destination IP address (usually 127.0.0.1).
No. The hosts file standard only maps IP addresses to hostnames. Ports are managed by your web server, reverse proxy, or the URL in your browser.
Your server may only be listening on the IPv6 loopback interface (::1), or you omitted the port number in the browser address bar for the custom domain.
Run curl -v http://mydomain.test:PORT in your terminal, or check active listeners with lsof -iTCP:PORT -sTCP:LISTEN on macOS and Linux.
Related Articles
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)
Docker container or Node.js app throwing getaddrinfo ENOTFOUND or EAI_AGAIN despite /etc/hosts? Fix Docker network isolation and IPv4 DNS precedence.
Sleezr Team
Developer tools team
Localhost Refused to Connect: How to Fix It
Fix "localhost refused to connect" (ERR_CONNECTION_REFUSED) in Chrome, on Windows, Mac, XAMPP and VS Code: check the server, the port, IPv6 vs IPv4, hosts file and firewall.
Sleezr Team
Developer tools team
Using Hosts Files for Docker Development on Mac
Configure hosts files for Docker, docker-compose and container networking. Map services to local domains and simplify Mac development.
Sleezr Team
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)
Fix NET::ERR_CERT_COMMON_NAME_INVALID when testing local domains in /etc/hosts. Learn mkcert SAN multi-domain certificates, Nginx and HSTS caveats.
Sleezr Team
Developer tools team
Fix EACCES: permission denied on /etc/hosts (2026)
Fix EACCES: permission denied when editing /etc/hosts in Node.js, CLI tools and shell scripts. Learn safe sudo elevation, tee syntax and permissions.
Sleezr Team
Developer tools team
Fix ERR_TOO_MANY_REDIRECTS with Hosts File (2026)
Infinite redirect loop (ERR_TOO_MANY_REDIRECTS) after hosts file edits? Fix WordPress siteurl, Nginx X-Forwarded-Proto, www mismatches and 301 caches.
Sleezr Team
Developer tools team