"WebSocket connection failed" after deploy: proxies, wss and idle timeouts
THE SHORT ANSWER
"WebSocket connection to 'wss://...' failed" after deploy means the handshake never reached your app or the app's answer never came back. Check four things in order. Use wss:// with the same host as the page, or a relative URL: browsers refuse ws:// from an https page. Make sure the proxy forwards the Upgrade and Connection headers; Nginx drops them unless you set them, while Traefik and Cloudflare pass them on their own. Serve the socket on the same port as your HTTP app, because hosts route one port. If connections open and then die after a minute or so, an idle timeout closed them: send pings every 25 to 30 seconds and reconnect on close. Platforms that sleep idle apps also close open sockets when they stop the app.
What does "WebSocket connection failed" mean?
A WebSocket starts as an ordinary HTTP request carrying Upgrade: websocket. The server has to answer 101 Switching Protocols; after that the same TCP connection carries WebSocket frames in both directions. Chrome prints this when the handshake gets anything else, or nothing:
WebSocket connection to 'wss://app.example.com/socket' failed:
The message often ends there, with no reason. Open the Network tab, filter by “WS” and click the request. The status code tells you which layer failed:
101, then a close: the socket worked and something closed it later. See the timeout section.200or an HTML page: the request reached a normal HTTP route, so a proxy dropped the Upgrade header.301or308: you connected tows://and the host redirects to HTTPS. WebSocket clients don't follow redirects.404: wrong path, or the socket server isn't attached to the HTTP server that got the request.502,503or no status: nothing answered at the address the proxy used.
A different error appears before any request is sent when an https page tries ws://:
SecurityError: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.
Quick checklist
| What you see | Likely cause | Fix |
|---|---|---|
SecurityError or mixed content | ws:// hard-coded in the client | Use wss:// or build the URL from location |
| Works locally, fails on the server | Client still points at localhost or a second port | Same host as the page, same port as the HTTP app |
| Status 200 or 400 instead of 101 | Proxy didn't forward Upgrade and Connection | Add the two proxy_set_header lines in Nginx |
| Drops after about 60 seconds of quiet | Proxy idle timeout | Ping every 25 to 30 seconds, or raise the timeout |
| Drops on every deploy, or at random | The process or proxy restarted | Reconnect with backoff in the client |
Is the client using the right URL?
Most “works locally” failures are the URL. In development the page is http://localhost:3000 and the socket is ws://localhost:3001, and both get copied into production. Three rules cover it:
- Match the page's scheme. An https page needs
wss://. Since theWebSocketconstructor acceptshttpandhttpsURLs and relative URLs, and converts them towsandwss, you can skip the scheme logic entirely. - Use the page's host, or an env var set to the real API domain, never a hard-coded
localhost. - Drop the extra port. Most hosts route one public port to one port in your app. A socket server listening on its own port is unreachable; attach it to the HTTP server instead.
// Browser: same host and scheme as the page
const ws = new WebSocket(new URL("/socket", location.href));
// Server (Node, ws package): share the HTTP server and its port
import http from "node:http";
import { WebSocketServer } from "ws";
const server = http.createServer(app);
const wss = new WebSocketServer({ server, path: "/socket" });
server.listen(process.env.PORT ?? 3000, "0.0.0.0");If the socket lives on a different domain than the page, as with a separate API, the browser doesn't apply CORS to WebSockets. Your server sees an Origin header and should check it itself. A failure there is your own code rejecting the handshake.
How do you proxy WebSockets through Nginx?
Upgrade and Connection are hop-by-hop headers, so Nginx doesn't pass them to the backend. Without them your app sees an ordinary GET and answers 200 or 400, and the browser reports the handshake as failed. Nginx's own example:
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1; # needed before nginx 1.29.7
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_read_timeout 3600s;
}
}
}The map sends Connection: upgrade only for requests that asked for one, so normal requests keep working. proxy_read_timeout is there because, per the docs, “the connection will be closed if the proxied server does not transmit any data within 60 seconds”. A chat that is quiet for a minute drops at exactly 60 seconds. Raise the timeout, or send pings so it never expires.
Traefik and Caddy proxy WebSockets without extra configuration. Cloudflare supports them on all plans as long as the record is proxied and the origin answers the upgrade.
Why does the connection close after a while?
Every layer between browser and app can close a connection that carries no data:
- Nginx, after 60 seconds by default (
proxy_read_timeout). - Cloudflare, after a period it doesn't publish. Its docs also say that when it releases new code it “may restart servers, which terminates WebSockets connections”.
- Load balancers, corporate proxies and mobile networks, on timers you can't see.
- Your own deploys and restarts, which end every connection to the old process.
So design for it: send a ping every 25 to 30 seconds, and reconnect when the socket closes. Socket.IO does both by default (the server pings every 25 seconds and the client reconnects). With the plain ws package, ping from the server and drop clients that stop answering:
wss.on("connection", (ws) => {
ws.isAlive = true;
ws.on("pong", () => { ws.isAlive = true; });
});
setInterval(() => {
for (const ws of wss.clients) {
if (!ws.isAlive) { ws.terminate(); continue; }
ws.isAlive = false;
ws.ping();
}
}, 30_000);On the client, reconnect in the close handler with a growing delay (1 s, 2 s, 4 s, capped at 30 s) so a restarting server isn't hit by every client at once.
What happens on EthioDeploy?
WebSockets work with no settings. Requests go through Cloudflare, then Traefik, then to your app on port 3000, and both layers pass the upgrade through. Things to get right:
- Use
wss://. Port 80 only redirects to HTTPS, so aws://connection gets a 301 and fails. - Serve the socket from the same process and port as your HTTP app (
$PORT, which is 3000). Only that port is routed. - Expect a close on every deploy. When the new version passes its health check, traffic moves to it and the old container is stopped, which closes its sockets. Clients that reconnect land on the new version.
Traefik itself didn't cut idle sockets in our test: with the same Traefik version and default timeouts, a connection that sent nothing for 200 seconds stayed open. Cloudflare in front can still close quiet connections, so keep the ping.
On the Free plan, apps sleep after 30 minutes without traffic, and an open WebSocket doesn't count as traffic. Activity is read from the proxy's access log, which records a WebSocket request only when it ends. A page that opens one socket and then talks over it for an hour will see the app stop after 30 minutes and the socket close. The first handshake to a sleeping app wakes it but doesn't complete; the client's next reconnect gets through. Reconnect logic handles both cases. If your app depends on long-lived connections, Pro and Business apps don't sleep. More in the troubleshooting docs and Deploy an Express app.
Questions
Usually the client URL still points at localhost or a separate port, or uses ws:// on an https page. Build the URL from the page's own host, use wss://, and attach the socket server to the same HTTP server and port as the rest of the app.
proxy_set_header Upgrade $http_upgrade and proxy_set_header Connection "upgrade" (or a map that sets it only for upgrade requests), plus proxy_http_version 1.1 on Nginx older than 1.29.7. Raise proxy_read_timeout or send pings, since Nginx closes a proxied connection after 60 seconds without data.
A proxy closed it for being idle. Nginx does this after 60 seconds without data by default. Send a ping every 25 to 30 seconds from the server, and reconnect from the client when the socket closes.
Yes, on all plans, for proxied records. Cloudflare closes connections that carry no data for a while and can drop connections when it restarts servers, so clients should ping and reconnect.
No. Sleep is based on requests in the proxy's access log, and a WebSocket is logged only when it closes. A Free app with no other traffic stops after 30 minutes and its sockets close. Pro and Business apps don't sleep.
Sources
Checked on November 29, 2026:
Push a repo. Get a URL.
Start freeJoin the EthioDeploy Telegram
Release notes as they ship, plus deploy help from the team and other developers.