Localhost refused to connect: How to fix the error

Votre note nous aide à améliorer nos contenus ! Partagez votre avis.

TL;DR: The “localhost refused to connect” error (ERR_CONNECTION_REFUSED) occurs when a browser cannot reach a local server due to a blocked firewall, incorrect IP address mapping, a stopped web server, corrupted DNS cache, a port conflict, or restrictive browser settings. Fix it by systematically checking each of these causes, starting with the simplest: confirming your server is actually running.

Few things derail a development workflow faster than a blank browser window showing “localhost refused to connect.” You start the server, open your browser, and nothing. Just a cold, unhelpful error message.

The good news: this error is almost always fixable in minutes. The bad news: it has multiple possible causes, which means there’s no single silver-bullet solution. The approach that works depends entirely on what’s blocking the connection in your specific environment.

This guide covers every major cause of the “localhost refused to connect” error including common gotchas in Docker and WSL2 and walks you through the exact steps to resolve each one on Windows, macOS, and Linux.

What Does “Localhost Refused to Connect” Actually Mean ?

Before diving into fixes, it helps to understand what’s happening technically.

The localhost hostname maps to the loopback network interface, which allows your machine to connect to itself without involving any external network hardware. By default, localhost resolves to the IP address 127.0.0.1 (or ::1 on IPv6 systems).

When you visit localhost in a browser and see the error, two distinct failure modes can be responsible:

  • ERR_CONNECTION_REFUSED : The server actively rejected the connection request
  • ERR_CONNECTION_TIMEOUT : The server didn’t respond at all

Both display the same “localhost refused to connect” message in most browsers, but they point to different underlying causes. A refused connection usually means the server isn’t listening on that port. A timeout often suggests a firewall is dropping packets silently.

➡️Localhost: what is it and how do I use it?

What Are the Main Causes of “Localhost Refused to Connect” ?

Here’s a quick summary of the six most common causes:

  1. Firewall blocking loopback traffic : Overly strict firewall rules can prevent your browser from reaching local services
  2. Wrong localhost IP address : The localhost hostname in your hosts file may not point to 127.0.0.1
  3. Web server not running : The server process (e.g., Apache, Nginx, Node.js) may have stopped or failed to start
  4. Corrupted DNS cache : Outdated or corrupt DNS records can prevent localhost from resolving correctly
  5. Port conflict or incorrect port : Another application may be using the same port, or you’re connecting to the wrong one
  6. Browser settings : Some browsers force HTTPS on localhost, causing the connection to fail

Now, let’s tackle each one.

How to Fix “Localhost Refused to Connect”

1. Check That Your Server Is Actually Running

Start here. It sounds obvious, but a stopped server is one of the most common culprits, especially after a system restart or a failed startup script.

On Linux (Ubuntu/Debian), check Apache’s status with:

sudo systemctl status apache2

On Rocky Linux or RHEL, the service is called httpd:

sudo systemctl status httpd

If the output shows inactive (dead), restart the service:

sudo systemctl restart apache2

For Node.js, Python, Django, or other framework-based servers, check your terminal for error output and restart the dev server manually. A simple missing dependency or syntax error can silently kill the process.

2. Temporarily Disable Your Firewall

A misconfigured firewall can block traffic on the loopback interface. To test whether this is the issue, temporarily disable it then re-enable it immediately after testing.

On Linux (Ubuntu/Debian):

sudo ufw disable

On Linux (Rocky Linux/RHEL):

sudo systemctl stop firewalld

On macOS: Go to System Settings → Network → Firewall and toggle it off.

On Windows: Open Control Panel → System and Security → Windows Defender Firewall → Turn Windows Defender Firewall on or off, then disable it for the private network.

If disabling the firewall resolves the error, reconfigure your firewall rules to explicitly allow traffic on the relevant port don’t leave it disabled permanently.

3. Verify the Localhost IP Address

localhost and 127.0.0.1 aren’t always interchangeable. On IPv6 systems, localhost may resolve to ::1 instead of 127.0.0.1. More critically, if someone has modified the hosts file on your machine, localhost could be pointing to an unexpected IP address.

Test this directly: type 127.0.0.1 (or your actual port, e.g., 127.0.0.1:8080) into your browser’s address bar. If that connects successfully but localhost doesn’t, the problem is a broken hostname mapping in your hosts file.

On Linux and macOS, the hosts file is located at /etc/hosts. On Windows, you’ll find it at C:\Windows\System32\drivers\etc\hosts. Open it with admin privileges and confirm the following line is present:

127.0.0.1 localhost

4. Flush Your DNS Cache

DNS caching speeds up browsing by storing the results of previous lookups. When those records get corrupted or stale, they can interfere with localhost resolution.

Flushing the DNS cache removes all stored records and forces a fresh lookup.

On Linux:

sudo /etc/init.d/nscd restart

On macOS:

sudo killall -HUP mDNSResponder

On Windows:

ipconfig /flushdns

After flushing, restart your browser and try connecting again.

5. Check for Port Conflicts

By default, web servers listen on port 80. If another application is already using that port, your server won’t be able to bind to it resulting in a refused connection.

On Linux or macOS, check which processes are using a specific port:

sudo lsof -i :80

On Windows, use:

netstat -ano | findstr :80

If a conflict exists, you have two options: stop the competing process or change your server’s port.

On XAMPP, open the XAMPP manager, go to Manage Servers → Apache Web Server → Configure, and set the port to something unused like 8080.

On WAMP, open C:\wamp\apache2\conf\httpd.conf, find the Listen directive, and change the port number from 80 to 8080. Update the ServerName value to match.

6. Fix Browser Settings (Chrome and Firefox)

Modern browsers aggressively enforce HTTPS. Chrome, in particular, uses a feature called HSTS (HTTP Strict Transport Security) that automatically redirects HTTP requests to HTTPS. When this applies to localhost, the redirect fails because your local server typically serves plain HTTP.

To fix this in Chrome:

  1. Type chrome://net-internals/#hsts in the address bar
  2. Select Domain Security Policy from the left menu
  3. Scroll to Delete domain security policies
  4. Enter localhost as the domain and click Delete
  5. Restart Chrome

To fix this in Firefox:

  1. Go to Settings → Privacy & Security
  2. Scroll to HTTPS-Only Mode
  3. Select Don’t enable HTTPS-Only Mode
  4. Restart Firefox

Also worth trying: open the same URL in a different browser. If it works in one but not another, the problem is browser-specific and this section is your answer.

How to Fix “Localhost Refused to Connect” in Docker

Docker introduces a specific networking complexity that trips up even experienced developers. Each Docker container runs in its own isolated network namespace meaning its 127.0.0.1 is completely separate from the host machine’s 127.0.0.1.

Here’s the practical consequence: if your application inside a container binds to 127.0.0.1, port forwarding won’t work. Docker’s -p 5000:5000 flag forwards traffic to the container’s external IP address (e.g., 172.17.0.2), not its loopback interface. Since the server is listening on the loopback interface and the traffic arrives at the external interface, the connection is refused.

The fix is to bind your server to 0.0.0.0 instead of 127.0.0.1. This tells the server to listen on all available network interfaces, including the one Docker uses for port forwarding.

For a Flask app, this looks like:

CMD [“flask”, “run”, “–host”, “0.0.0.0”]

For Python’s built-in HTTP server:

python3 -m http.server –bind 0.0.0.0

Other frameworks have equivalent configuration options. Once your app listens on 0.0.0.0, Docker’s port forwarding will route traffic to it successfully.

➡️HTTP Error Codes Guide: 400, 403, 404, 500 Explained (and How to Fix Them)

How to Fix “Localhost Refused to Connect” in WSL2

WSL2 (Windows Subsystem for Linux 2) adds another layer of complexity. Unlike WSL1, WSL2 runs a full Linux virtual machine with its own network stack. While WSL2 includes a localhost forwarding feature that generally allows Windows to access services running inside WSL2, it can fail under certain conditions.

One common cause: a Windows service called IP Helper (iphlpsvc) can reserve specific ports including common development ports like 3000 before your WSL2 server starts. When your server tries to bind to that port, it either fails silently or gets forwarded to the wrong process.

Diagnostic steps:

  1. Check if the port is occupied on the Windows side:
    netstat -ano | findstr :3000
  2. Identify the process using that port via Task Manager → Services
  3. If IP Helper is holding the port, try running wsl –shutdown in a Windows terminal and restarting WSL2

Alternative approaches if the issue persists:

  • Change your app’s port (e.g., from 3000 to 3006) to avoid the conflict
  • Enable WSL2’s mirrored networking mode, which allows Windows and WSL2 to share localhost more seamlessly (available in recent Windows 11 builds via .wslconfig)

For services running inside WSL2 that need to call a Windows host application, use host.docker.internal or the Windows host’s actual IP address rather than localhost.

Troubleshooting Checklist: Stop Guessing, Start Diagnosing

Work through this checklist in order to isolate the issue fast:

  • Is the server process actually running?
  • Does connecting via 127.0.0.1 work when localhost doesn’t?
  • Is the firewall blocking the port?
  • Is the DNS cache corrupted? (Flush it)
  • Is the correct port number being used?
  • Is another application occupying the same port?
  • Is the browser forcing HTTPS on a plain HTTP server?
  • Are you running Docker and binding to 127.0.0.1 instead of 0.0.0.0?
  • Are you on WSL2 with a port reserved by Windows services?

Methodical elimination is far faster than guessing. Start at the top the most common fix is often the first check.

Get Back to Building

The “localhost refused to connect” error is a solvable problem, every single time. Whether it’s a paused server, a misconfigured firewall, a Docker networking quirk, or a browser enforcement rule, each cause has a clear remedy.

The key is to treat it as a diagnostic process. Confirm your server is running, verify the IP resolution, rule out port conflicts, and check your environment-specific settings (Docker, WSL2, browser). One of these will be the answer.

Once your environment is stable, consider documenting the fix internally. These errors have a way of reappearing across team members or after system updates and a clear rundown of what worked saves everyone time.

Frequently Asked Questions

What does “localhost refused to connect” mean?

“Localhost refused to connect” means the browser sent a connection request to localhost (the loopback address 127.0.0.1) and either received an active refusal (ERR_CONNECTION_REFUSED) or no response at all (ERR_CONNECTION_TIMEOUT). The most common cause is that no server process is listening on the expected port.

Why does localhost work in one browser but not another?

Different browsers handle HTTP/HTTPS enforcement differently. Chrome’s HSTS policy can automatically redirect localhost HTTP requests to HTTPS, causing the connection to fail if your server only serves HTTP. Try disabling HTTPS-only mode or clearing Chrome’s HSTS settings for localhost.

How do I fix “localhost refused to connect” in Docker?

Bind your server to 0.0.0.0 instead of 127.0.0.1. Docker’s port forwarding routes traffic to a container’s external IP address not its loopback interface. A server listening only on 127.0.0.1 inside the container will never receive that forwarded traffic.

Why does localhost connection get refused in WSL2?

WSL2 runs in a separate virtual machine with its own network stack. A Windows system service called IP Helper can reserve ports before your WSL2 server starts, blocking the connection. Running wsl –shutdown and restarting, or switching to a different port, typically resolves this.

How do I check which process is using a port on Windows?

Run netstat -ano | findstr :<port> in Command Prompt (replace <port> with your target port number, e.g., 3000). The output includes the PID of the process holding the port. Cross-reference that PID in Task Manager to identify the application.

Does flushing DNS fix localhost connection issues?

It can. Corrupted DNS cache entries can prevent localhost from resolving correctly to 127.0.0.1. Flushing the cache forces the system to perform a fresh DNS lookup and often resolves stubborn connection issues that persist after other fixes.

Plus de Systalink

Collaborative Tools for Remote Work

30 Collaborative Tools for Remote Work in 2026

Cloud Cybersecurity

Cloud cybersecurity explained : How it works and why it matters