Learning objectives
After reading this article you will be able to:
- Understand what each error means in TCP: an instant refusal or silence
- Diagnose the cause with curl, nc, ss and Test-NetConnection
- Fix a stopped process, a wrong port, a 127.0.0.1 bind and firewall rules
Both errors open the same page in the browser and look like the same thing: the site won't load. Underneath, they are opposites. In one, the server answered right away and said "no". In the other, nobody answered at all.
That difference saves time, because each one points somewhere else. A refusal is almost always a server problem: a stopped process, the wrong port, the listen address. Silence is almost always a path problem: a firewall, the IP, routing.
What do the two errors look like?#
In Chrome:
This site can't be reached
localhost refused to connect.
ERR_CONNECTION_REFUSEDThis site can't be reached
example.com took too long to respond.
ERR_CONNECTION_TIMED_OUTOutside the browser, the same pair shows up under other names. In Node.js, Error: connect ECONNREFUSED 127.0.0.1:3000 and Error: connect ETIMEDOUT 203.0.113.10:443. In Python on Linux, ConnectionRefusedError: [Errno 111] Connection refused and TimeoutError: [Errno 110] Connection timed out. In curl, exit codes 7 (couldn't connect) and 28 (timed out).
What happens in TCP in each case?#
A TCP connection starts with three messages: the client sends a SYN, the server answers SYN-ACK and the client confirms with ACK. Both errors happen at this first step, before any HTTP.
Refused. The SYN reached a machine that exists, but no process listens on that port. RFC 9293 tells the system to answer with a reset (RST), and the client gives up immediately. The Linux connect man page sums it up: no one listening on the remote address. A firewall set to reject (REJECT) also answers right away, with an error packet, and the client usually sees the same refusal.
Timed out. The SYN got lost on the way or was dropped, and nothing came back. The client resends the SYN at growing intervals. On Linux, the default tcp_syn_retries is 6, which means retrying for up to about 127 seconds; browsers and libraries usually give up earlier, with their own timeout. A firewall set to drop (DROP) produces exactly this.
Similar connection errors, for comparison:
| Chrome error | What the client got | Where to look |
|---|---|---|
ERR_CONNECTION_REFUSED |
An instant refusal (RST or port error) | Process, port and listen address |
ERR_CONNECTION_TIMED_OUT |
Nothing, until time ran out | Firewall, IP in DNS, routing, server off or overloaded |
ERR_CONNECTION_RESET |
An RST after the connection already existed | A process that crashed mid-response, a proxy, a firewall |
ERR_ADDRESS_UNREACHABLE |
A notice that there is no route to the IP | Network, wrong IP |
DNS_PROBE_FINISHED_NXDOMAIN |
No IP for the name | DNS, before any connection |
The last one never reaches TCP; it's covered in DNS_PROBE_FINISHED_NXDOMAIN. And if the connection opens but the browser gets one redirect after another, the problem is already at the HTTP level: see ERR_TOO_MANY_REDIRECTS.
What causes ERR_CONNECTION_REFUSED?#
From most to least common:
- The process isn't running. The dev server was closed, crashed on startup or is still compiling. In production, the app crashed and didn't come back. The logs show the startup error.
- Wrong port. The app listens on 3000 and you open 8080, or the front end calls the API on an old port. Vite, for example, jumps to the next free port when the configured one is taken.
- Listening on 127.0.0.1 only. The server accepts connections only from the same machine (or the same container). From another computer, another container or the host's proxy, the port looks closed. Listen on
0.0.0.0, as explained in EADDRINUSE and 502 after deploy. - HTTPS on a port without HTTPS. You typed
https://(or the browser forced it through HSTS) for a server that only speaks HTTP on port 80. Port 443 is closed, so the connection is refused. - A container without a published port. In Docker, the process listens inside the container, but the port wasn't published on the host with
-p. - A firewall with
REJECT. The process has the port open, but a firewall rule rejects the connection.
The same error with databases has its own causes (service stopped, bind-address, the engine's default port), covered in ECONNREFUSED on a database.
What causes ERR_CONNECTION_TIMED_OUT?#
- A firewall dropping the port. This is the classic case on a freshly created VPS: the app listens, but the system firewall (ufw, iptables) or the cloud provider's firewall doesn't allow the port.
- DNS pointing to the wrong IP. After a migration, the domain still resolves to the old server, which is off or has nothing on the port. Compare the DNS IP with the server's and see DNS propagation.
- The server is off or offline. With no machine to answer, there is no RST: only silence.
- The server is overloaded. With the connection queue full, new SYNs can be dropped. The
connectman page mentions this case: the server may be too busy to accept new connections. - The client's network blocks it. Corporate networks, antivirus software and VPNs block ports or destinations. If it works from another network, the server is fine.
How do you diagnose it with curl, nc and ss?#
Start on the client side, testing the exact port:
curl -v --connect-timeout 5 http://example.com:8080/The last line tells you which case it is. The wording changes between curl versions, but the code in parentheses doesn't:
curl: (7) Failed to connect to example.com:8080 after 0 ms: Could not connect to server
curl: (28) Connection timed out after 5002 millisecondsA (7) after a short time is a refusal. A (28) after the 5 seconds of --connect-timeout is silence.
To test only the port, without HTTP, use nc (netcat):
nc -vz -w 5 example.com 8080-z only checks whether something is listening, -v prints the result and -w sets the timeout in seconds. The answer is succeeded, Connection refused or a timeout notice; the exact message varies between netcat versions. In the OpenBSD version, a refusal looks like this:
nc: connect to example.com port 8080 (tcp) failed: Connection refusedOn Windows, PowerShell has Test-NetConnection:
Test-NetConnection example.com -Port 8080Look for the TcpTestSucceeded line: True means the port accepted the connection.
If you confirmed a refusal, go to the server and see who listens, and on which address:
sudo ss -ltnpState Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 127.0.0.1:3000 0.0.0.0:* users:(("node",pid=4121,fd=21))127.0.0.1:3000 only accepts local connections; 0.0.0.0:3000 or *:3000 accepts them on any interface. If the port doesn't show up at all, the process isn't listening. Run curl -v http://127.0.0.1:3000/ on the server itself: if it works there and not from outside, the problem is the listen address or the firewall.
If you got a timeout, check the path: dig +short example.com (or nslookup example.com on Windows) to confirm the IP belongs to the right server, and the system and provider firewall rules for the port.
Why does the error become a 502 or 504 behind a proxy?#
When there's a CDN, a load balancer or a reverse proxy in front, the browser connects to the proxy, not to the app. The proxy is up, so the browser sees neither a refusal nor a connection timeout. The proxy sees it, when it tries to reach the app, and translates the problem into HTTP: a refusal usually becomes 502 Bad Gateway, and no answer becomes 504 Gateway Timeout.
That changes the diagnosis. If your hosted domain shows ERR_CONNECTION_REFUSED or ERR_CONNECTION_TIMED_OUT, the problem sits before the proxy: DNS pointing elsewhere, a mistyped port or the client's network. If it shows a 502 or 504, the proxy is fine and the app didn't accept the connection. The overview of those codes is in 500, 502, 503 and 504 errors, and each layer's timeouts in load balancer timeouts.
How to do this on Square Cloud#
On Square Cloud, web traffic goes through an Anycast CDN with more than 200 points of presence before it reaches your app. A web app must listen on 0.0.0.0 on the port in the PORT variable, which the platform sets along with HOST. Only port 80 receives traffic, published over HTTPS and WSS on the *.squareweb.app subdomain (or your own domain, from the Standard plan), so an app listening on a fixed port such as 3000 gets no requests at all. Bots, which have no subdomain, receive no inbound traffic.
The network waits up to 30 seconds to connect to your app, and the proxy doesn't cut long-lived connections such as WebSocket and SSE. If the process stops, the real-time logs show the startup error. With automatic restart on (it's on by default for deploys from the dashboard), an app that exits with an error comes back by itself. Running out of memory stops the app, and it doesn't come back by itself: raise the RAM and start it again.
For connections going out of your app, outbound traffic on port 22 isn't allowed, so an SSH or SFTP client can't reach a server on that port. The network uses dynamic IPv4 only, with no fixed IP and no IPv6, so there is no stable address to add to an external service's IP allowlist. See the network details.
Network
Custom domains, automatic TLS and a global CDN for your app
FAQs
What is the difference between ERR_CONNECTION_REFUSED and ERR_CONNECTION_TIMED_OUT?
With refused, the target machine answers right away and turns the connection down: it exists, but nothing listens on that port. With timed out, no answer arrives before the browser gives up, which points to a firewall, routing or a wrong address.
Why did localhost refuse to connect?
Almost always the dev server isn't running, crashed on startup or listens on a different port. Check in the terminal that it started and which port the log reports, then test that same port with curl.
Can the error be on my network only?
Yes. A corporate firewall, antivirus, VPN or a local DNS cache holding an old IP can block or divert the connection. Test from another network, such as your phone's mobile data: if it works there, the problem is on the path, not the server.
Why do I get a 502 instead of connection refused behind a proxy?
Because the browser connects to the proxy, which is up. The proxy is the one that gets refused when it tries to reach the app, and it answers the browser with 502 Bad Gateway.
More in Networking and domains
See all 31 articles- What is a load balancer and how does it work?A load balancer spreads incoming requests across several servers. Learn how it works, the main algorithms, layer 4 vs 7, and a working nginx example.
- DNS records explained: A, CNAME, MX, TXTDNS records are the instructions in a zone that tell the internet where a name points. Learn A, AAAA, CNAME, MX, TXT, NS and CAA with examples and dig commands.
- How to point a domain to your server or appTo point a domain to your server or app, create a CNAME (or A) record in your registrar's DNS panel. Steps, how to test it, and common mistakes.
- ERR_TOO_MANY_REDIRECTS: redirect loops explainedERR_TOO_MANY_REDIRECTS is a redirect loop, most often an app forcing HTTPS behind a proxy. Learn how to trace the chain with curl and fix each cause.