Browser: WebSocket connection to 'wss://example.com/socket' failed: Error during WebSocket handshake: Unexpected response code: 400 (nginx proxy)
Nginx isn't forwarding the Upgrade/Connection headers needed for WebSockets.
WebSocket connection failed: Error during WebSocket handshake: Unexpected response code: 400 nginxWebSocket connection to wss:// failed nginx proxy Upgrade headerWebSocket connection failed: Error during WebSocket handshake: Unexpected response code: 200nginx websocket 502 Bad Gateway upgradeSocket.IO behind nginx 400 Bad Request Meaning
Add to the proxy location: proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; and raise proxy_read_timeout so idle sockets aren't closed. Socket.IO, Vite HMR, Next.js dev and ActionCable need this.
Common causes
- Missing
proxy_set_header Upgrade/Connection - HTTP/1.0 proxying (default)
proxy_read_timeouttoo short (60s)- Wrong location path
- CDN/load balancer not allowing WebSockets
⚡ Quick fix
- Add the Upgrade/Connection headers and
proxy_http_version 1.1; - Increase
proxy_read_timeout 3600s; - Verify CDN/ALB WebSocket support
Detailed fix by platform
Nginx
location /socket { proxy_pass http://app; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 3600s; }
How to diagnose
- Headers — Upgrade forwarded?
- HTTP version — 1.1?
- Timeout — Long enough?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- 400 HTTP 400 Bad Request
- Cloudflare 502 Cloudflare-branded 502 Bad Gateway / 504 Gateway Timeout ("cloudflare" footer)
- connect() failed (111: Connection refused) while connecting to upstream Nginx: [error] connect() failed (111: Connection refused) while connecting to upstream, upstream: "http://127.0.0.1:3000/"
Most viewed in Nginx / Apache
- nginx: [emerg] unknown directive Nginx: nginx: [emerg] unknown directive "x" in /etc/nginx/sites-enabled/x:12 — configuration file test failed
- upstream timed out Nginx: upstream timed out (110: Connection timed out) while reading response header from upstream
- AH00558 Apache: AH00558: apache2: Could not reliably determine the server's fully qualified domain name
Other ways to find it
Where this error sits
Origin: Edge & web server. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026