Networking
Connection Timed Out? Causes and How to Fix It on a Server
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربي"Connection timed out" is one of the most common and least informative server errors. It can mean anything from a closed firewall port to a crashed application or a slow database query. The good news is that a timeout always means the same basic thing: one side waited for an answer and gave up. If you work out where the waiting happened, the cause is usually easy to find. This guide walks through the types of timeouts and a systematic way to fix them.
Timeout vs refused vs reset
The exact error message tells you a lot:
| Error | What happened | Usual cause |
|---|---|---|
| Connection timed out | No reply at all to the connection attempt | Firewall silently dropping, wrong IP, server down, routing issue |
| Connection refused | The host replied that nothing listens on that port | Service not running, listening on another port or only on 127.0.0.1 |
| Connection reset | The connection was opened, then forcibly closed | App crashed, proxy or firewall killed it, protocol mismatch |
| Read timed out / 504 | Connected fine, but the response took too long | Slow application, database, or upstream service |
The key distinction: a connect timeout happens before the connection exists, so it is almost always a network or firewall question. A read timeout happens after connecting, so it is almost always an application performance question.
Step-by-step diagnosis
1. Is the host reachable?
ping -c 5 your-server-ip
mtr -rwc 50 your-server-ipIf ping fails, it may just be blocked, so do not conclude the server is down. Check the next step. If mtr shows heavy loss that continues to the final hop, the network path itself is part of the problem.
2. Is the port open from outside?
nc -vz -w 5 your-server-ip 443succeeded/open: the port is reachable; the problem is higher up.refused: the host is reachable but nothing is listening.- Timeout: something is dropping the packets, usually a firewall.
On Windows, Test-NetConnection your-server-ip -Port 443 in PowerShell gives the same answer.
3. Is the service listening, and on which address?
On the server:
sudo ss -tlnpCheck the Local Address column. 0.0.0.0:443 or [::]:443 means it listens on all interfaces. 127.0.0.1:3306 means it accepts only local connections, which is the default for MySQL and many other services. Remote clients will get refused or time out until you change the bind address and the firewall rules, ideally allowing only trusted IPs.
4. Check every firewall layer
There can be more than one firewall between the client and your app:
- The cloud firewall in your provider's control panel.
- The server's own firewall:
sudo ufw status verbose, orsudo nft list ruleset, orsudo iptables -L -n -v. - Windows Firewall on Windows servers.
- Network restrictions on the client side: corporate networks often block uncommon outgoing ports.
A firewall that uses DROP produces a timeout; one that uses REJECT produces "refused". If you allow a port in ufw but forget the cloud firewall, the connection still times out.
5. Is the server overloaded?
A server that is out of memory, swapping heavily or at 100 percent CPU can accept connections very slowly or not at all. Check:
uptime
free -h
dmesg -T | grep -iE "oom|conntrack|syn flooding""possible SYN flooding" in the kernel log means the listen queue overflowed; the service could not accept connections fast enough. "nf_conntrack: table full" means the connection tracking table is full and new connections are dropped.
6. Read timeouts: find the slow part
If the connection succeeds but the request times out, measure it:
curl -o /dev/null -s -w "connect: %{time_connect}\nttfb: %{time_starttransfer}\ntotal: %{time_total}\n" https://example.com/slow-pageFast connect with slow TTFB means the app is slow. Check application logs, slow query logs (slow_query_log in MySQL, log_min_duration_statement in PostgreSQL) and any external APIs the app calls. Our article on server latency explains how to break down where the time goes.
Fixing Nginx 504 Gateway Timeout
When Nginx proxies to an application (Node.js, PHP-FPM, Python), it waits a limited time for the upstream. The default for proxy_read_timeout is 60 seconds. If the app regularly takes longer, you get 504 errors.
location / {
proxy_pass http://127.0.0.1:3000;
proxy_connect_timeout 10s;
proxy_send_timeout 60s;
proxy_read_timeout 120s;
}For PHP-FPM the equivalent is fastcgi_read_timeout, and PHP itself has max_execution_time and the pool setting request_terminate_timeout. All of them must allow the longer duration.
Raising timeouts is a short-term fix. A request that takes minutes should usually become a background job processed from a queue.
SSH sessions that drop when idle
Idle SSH sessions are often cut by NAT routers or firewalls that forget inactive connections. Send keepalives from the client in ~/.ssh/config:
Host *
ServerAliveInterval 30
ServerAliveCountMax 4Or on the server in /etc/ssh/sshd_config with ClientAliveInterval 60 and ClientAliveCountMax 3, then restart the SSH service.
Checklist
- Read the exact error: timed out, refused, reset or 504.
- Test reachability and the port with
ping,mtrandnc. - Confirm the service listens on the right address with
ss -tlnp. - Check the cloud firewall and the server firewall.
- Look for overload, OOM and conntrack messages.
- For read timeouts, profile the app and database; adjust proxy timeouts only where justified.
If you need to work on a server whose network is blocked by a firewall mistake, a browser console lets you log in without SSH. You can deploy a VPS with console access included.
FAQ
Why does my site time out for some users but not others?
It is often a routing or network issue on specific ISPs, an IP-based firewall or rate limit catching certain users, or a DNS record pointing some users to an old server. Ask affected users for their location and an mtr report, and check your firewall logs.
Should I just increase the timeout values?
Only after you know why requests are slow. A higher timeout hides the symptom while users still wait. Fix the slow query or move long work to a background job, then set timeouts slightly above normal response times.
What is the difference between a connect timeout and a read timeout?
A connect timeout is how long to wait for the connection to open. A read timeout is how long to wait for data after connecting. Connect timeouts point to the network or firewall; read timeouts point to the application or its dependencies.