Localhost Refused to Connect? Fix It Now
In this article
If your browser shows “localhost refused to connect,” it means the web server you’re trying to reach on your own computer isn’t running, or it’s listening on a different port than the one you typed. The fix is usually to start the server, check the port number, or adjust your firewall—here’s how to diagnose and solve it step by step.
What “localhost refused to connect” actually means
When you type localhost or 127.0.0.1 into a browser, your computer sends a request to itself, not to the internet. The browser expects a program (like Apache, Nginx, Node.js, or Python’s built-in server) to be listening on a specific port—usually port 80 or 3000. If no program is listening, the operating system rejects the connection, and the browser shows “refused to connect.” This is different from “timed out,” which means something is listening but not responding, or “can’t be reached,” which usually points to a network issue.
Why this happens: common causes
- Server not started – The most common cause. You haven’t launched your development server, or it crashed.
- Wrong port – The server is running on port 3000, but you’re trying to access port 80, or vice versa.
- Firewall or security software – Your OS firewall or antivirus may be blocking incoming connections to localhost.
- Hosts file misconfiguration – The
hostsfile (on Windows, macOS, Linux) may have an incorrect entry for localhost. - Proxy settings – A system or browser proxy can intercept localhost requests and cause them to fail.
- IPv6 vs IPv4 – Sometimes
localhostresolves to::1(IPv6) while your server only listens on127.0.0.1(IPv4).
How to fix it: step-by-step
- Start your server. If you’re using a tool like XAMPP, WAMP, or MAMP, open its control panel and click “Start” for Apache/MySQL. For Node.js, run
node app.jsin the project folder. For Python, runpython -m http.server 8000. - Check the port. Look at your server’s output—it usually prints something like “listening on port 3000.” Then access
http://localhost:3000(replace 3000 with the actual port). - Verify with a command. Open a terminal or command prompt and run
netstat -ano | findstr :3000(Windows) orlsof -i :3000(macOS/Linux). If nothing appears, the server isn’t listening. - Test with curl. Run
curl http://localhost:3000in a terminal. If you get a response, the issue is browser-specific (try a different browser or clear cache). If you get “connection refused,” the server isn’t running. - Disable proxy settings. In Chrome, go to Settings → System → Open your computer’s proxy settings, and ensure “Use a proxy server” is off (or add localhost to the bypass list).
- Check the hosts file. Open
C:\Windows\System32\drivers\etc\hosts(Windows) or/etc/hosts(macOS/Linux). Ensure there’s a line like127.0.0.1 localhost. If it’s missing, add it and save. - Flush DNS. Run
ipconfig /flushdns(Windows) orsudo dscacheutil -flushcache(macOS) to clear any stale DNS entries. - Try 127.0.0.1 instead of localhost. If localhost fails but 127.0.0.1 works, it’s an IPv6/hosts issue. Conversely, if 127.0.0.1 fails, your server may be bound to a specific IP.
When the fix doesn’t work: deeper issues
If you’ve tried the above and still get “refused,” consider these:
- Port already in use. Another process may be occupying the port. Run
netstat -ano | findstr :3000to see the PID, then kill it withtaskkill /PID(Windows) or/F kill -9(macOS/Linux). - Server binding to 127.0.0.1 only. Some servers (like older Node.js versions) bind only to IPv4. If your browser tries IPv6 first, it may fail. Use
localhostwith the port, or start the server with--host 0.0.0.0to listen on all interfaces. - Docker or virtual machines. If you’re running a container, the port may not be mapped correctly. Check with
docker psand ensure the port is published (e.g.,-p 8080:80). - Firewall rules. On Windows, allow the specific port in Windows Defender Firewall → Advanced Settings → Inbound Rules. On macOS, check System Settings → Network → Firewall.
- Corrupted browser cache. Try a private/incognito window. If it works, clear your browser’s cache and cookies.
Preventing future “refused to connect” errors
- Always start your server before opening the browser.
- Use a consistent port, and bookmark the full URL (e.g.,
http://localhost:3000). - If you use multiple projects, consider a tool like
nodemonto auto-restart Node.js servers on file changes. - Add localhost to your browser’s proxy bypass list to avoid proxy interference.
- Keep your hosts file clean—don’t add extra entries for localhost unless you know what you’re doing.
Frequently Asked Questions
Why does localhost work but 127.0.0.1 doesn’t (or vice versa)?
This usually points to an IPv6/IPv4 mismatch. localhost may resolve to ::1 (IPv6) while your server binds to 127.0.0.1 (IPv4). Try using the IP that matches your server’s binding, or configure your server to listen on both.
Can I fix this by changing my hosts file?
Yes, if the hosts file has a wrong entry for localhost, it can cause this error. Ensure it contains 127.0.0.1 localhost and ::1 localhost (for IPv6). But if the server isn’t running, editing the hosts file won’t help.
What port should I use for localhost?
It depends on your server. Common defaults: 80 (Apache/Nginx), 3000 (React/Node), 5000 (Flask), 8000 (Python). Check your server’s documentation or output to see which port it uses.
Why does localhost work in one browser but not another?
This is often caused by browser-specific proxy settings or extensions. Try disabling extensions, or use a different browser. If it works in incognito mode, clear your cache and cookies.
Is it safe to disable my firewall for localhost?
It’s not necessary. Instead, add an inbound rule to allow the specific port (e.g., 3000) for local connections. Disabling the firewall entirely exposes your computer to network threats.
Can I access localhost from another device?
Yes, but you need to use your computer’s LAN IP (e.g., 192.168.1.5) instead of localhost, and ensure the server is listening on 0.0.0.0 (not just 127.0.0.1). Also, the firewall must allow the port on the network interface.