# Browser: WebSocket connection to 'wss://example.com/socket' failed: Error during WebSocket handshake: Unexpected response code: 400 (nginx proxy)

**Code:** `WebSocket connection failed through Nginx (missing Upgrade headers)` · **Category:** Nginx / Apache · **Seen on:** Nginx, JavaScript

Nginx isn't forwarding the `Upgrade`/`Connection` headers needed for WebSockets.

## 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_timeout` too short (60s)
- Wrong location path
- CDN/load balancer not allowing WebSockets

## Quick fix

1. Add the Upgrade/Connection headers and `proxy_http_version 1.1;`
2. Increase `proxy_read_timeout 3600s;`
3. Verify CDN/ALB WebSocket support

## Detailed fix by platform

### Nginx

1. `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

1. **Headers** — Upgrade forwarded?
2. **HTTP version** — 1.1?
3. **Timeout** — Long enough?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/nginx-apache/nginx-websocket-upgrade-headers-missing)
