Cloudflare WebSocket connection fails in the browser but works in Postman
Budget: €8 – €30 EUR
Hi everyone,
I'm trying to set up a WebSocket server behind Nginx on port 80 (HTTP) to support both ws:// and wss:// connections. However, while the WebSocket works in Postman, it fails when trying to connect via wss:// in a browser. The console error is:
WebSocket connection to 'wss://mydomain.com/websocket_endpoint' failed:
WebSocket Error: Event { isTrusted: true, type: "error", target: WebSocket, ... }
Nginx configuration (simplified):
location /websocket_endpoint {
proxy_pass http://internal-server-ip:port/websocket_path;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
✅ Connecting browser via ws://mydomain.com/websocket_endpoint → Fails in browser
✅ Connecting browser via wss://mydomain.com/websocket_endpoint → Fails in browser
✅ Works in Postman when connecting via wss://mydomain.com/websocket_endpoint
✅ Works in Postman when connecting via ws://mydomain.com/websocket_endpoint
✅ Ensured Cloudflare WebSockets is enabled and "Always Use HTTPS" is disabled
I'm trying to set up a WebSocket server behind Nginx on port 80 (HTTP) to support both ws:// and wss:// connections. However, while the WebSocket works in Postman, it fails when trying to connect via wss:// in a browser. The console error is:
WebSocket connection to 'wss://mydomain.com/websocket_endpoint' failed:
WebSocket Error: Event { isTrusted: true, type: "error", target: WebSocket, ... }
Nginx configuration (simplified):
location /websocket_endpoint {
proxy_pass http://internal-server-ip:port/websocket_path;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
✅ Connecting browser via ws://mydomain.com/websocket_endpoint → Fails in browser
✅ Connecting browser via wss://mydomain.com/websocket_endpoint → Fails in browser
✅ Works in Postman when connecting via wss://mydomain.com/websocket_endpoint
✅ Works in Postman when connecting via ws://mydomain.com/websocket_endpoint
✅ Ensured Cloudflare WebSockets is enabled and "Always Use HTTPS" is disabled