Common Nginx Error Log Pattern (TLS / WSS WebSockets)
When upstream sockets, timeouts, body limits, or Docker DNS rules are misconfigured for TLS / WSS WebSockets, Nginx typically records an entry matching the log line below inside /var/log/nginx/error.log:
2026/11/28 13:05:41 [info] 6190#6190: *3412 client prematurely closed connection while reading upstream, client: 198.51.100.88, server: wss.example.com, request: "GET /ws HTTP/1.1", upstream: "http://127.0.0.1:8080/ws", host: "wss.example.com"
Reverse Proxy Architecture & Header Forwarding for TLS / WSS WebSockets
Terminating TLS on port 443 in Nginx and forwarding secure WebSockets (wss://) to an internal ws:// upstream service is a standard real-time architecture, yet two misconfigurations consistently break production traffic: hardcoding proxy_set_header Connection "upgrade" unconditionally on mixed HTTP/WebSocket locations, and leaving Nginx default HTTP/1.0 + 60-second timeout settings intact.
When Connection "upgrade" is hardcoded on a server block that also serves normal REST GET/POST endpoints or health checks, Nginx forwards an invalid Connection: upgrade header without an accompanying Upgrade header on ordinary HTTP requests. Strict upstream frameworks (Go net/http, Rust Axum/Hyper, Python Uvicorn, and Envoy sidecars) can reject or mishandle those connections instead of reusing clean HTTP/1.1 keep-alive sockets.
The Nginx-recommended pattern defines a top-level map $http_upgrade $connection_upgrade block inside the http {} context. Whenever a browser initiates a wss:// upgrade handshake carrying Upgrade: websocket, $connection_upgrade evaluates to upgrade; for every regular HTTPS request where $http_upgrade is empty, it cleanly evaluates to close (or empty string for keepalive upstreams).
To keep idle wss:// tunnels alive without 60-second drops, pair the map directive with proxy_http_version 1.1, proxy_set_header Upgrade $http_upgrade, proxy_set_header Connection $connection_upgrade, proxy_buffering off, and matching proxy_read_timeout and proxy_send_timeout values (such as 3600s) alongside periodic server ping frames.
- Routing /ws/ alongside REST /api/ microservices on one TLS server block? Check our Nginx multi-service subpath routing guide.
- Proxying wss:// to containerized upstreams? Prevent 502 errors after container redeploys with our Nginx Docker Compose upstream proxy setup.
- Running Socket.IO clusters or Odoo gevent WebSockets? Compare sticky routing in our Socket.IO WebSocket Nginx reverse proxy config.
Step-by-Step Nginx & TLS / WSS WebSockets Deployment Guide
Apply the Nginx server block above alongside these upstream framework trust-proxy settings so HTTPS detection, real client IPs, and reload safety work reliably in production.
1Broken Unconditional Upgrade vs. Hardened TLS 443 + $connection_upgrade Map
/etc/nginx/sites-available/wss.example.com.confPlace the map $http_upgrade $connection_upgrade block in the http context (outside server {}), then reference $connection_upgrade inside your TLS 443 location block alongside HTTP/1.1 and extended read/send timeouts.
/etc/nginx/sites-available/wss.example.com.confnginx# BROKEN PATTERN (breaks standard HTTP/1.1 calls or drops idle wss:// every 60s): # location /ws { # proxy_pass http://127.0.0.1:8080; # proxy_set_header Connection "upgrade"; # Missing map, proxy_http_version 1.1 & timeouts # } map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 443 ssl http2; server_name wss.example.com; ssl_certificate /etc/letsencrypt/live/wss.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/wss.example.com/privkey.pem; location /ws { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 3600s; proxy_send_timeout 3600s; proxy_buffering off; } }2Use Empty String Instead of close When Combining WSS With Upstream Keepalive
/etc/nginx/conf.d/websocket-map.confIf your upstream {} block enables keepalive 32; for high-throughput HTTP requests alongside WebSockets, map empty $http_upgrade to "" instead of close so non-WebSocket calls preserve upstream keep-alive connections.
/etc/nginx/conf.d/websocket-map.confnginxmap $http_upgrade $connection_upgrade { default upgrade; '' ""; } upstream realtime_backend { server 127.0.0.1:8080; keepalive 32; }3Test TLS 443 WSS Handshake (HTTP 101) and Regular HTTPS Requests Together
terminalReload Nginx and confirm both an upgraded WebSocket request (101 Switching Protocols) and a regular HTTPS request (200 OK) succeed cleanly on the same server block.
terminalbashsudo nginx -t && sudo systemctl reload nginx curl -i -N \ -H "Connection: Upgrade" \ -H "Upgrade: websocket" \ -H "Sec-WebSocket-Version: 13" \ -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" \ https://wss.example.com/ws
Common TLS / WSS WebSockets Nginx Reverse Proxy Pitfalls
Review these three production failure modes before reloading Nginx on staging or live servers.
Placing map $http_upgrade $connection_upgrade Inside server {} Instead of http {}
Nginx rejects map directives inside server {} or location {} blocks with "map directive is not allowed here". Always define map at the top level of your sites-available file (which nginx.conf includes inside http {}) or in /etc/nginx/conf.d/.
Raising proxy_read_timeout Without Raising proxy_send_timeout
Both proxy_read_timeout and proxy_send_timeout default to 60s. Raising only read timeout can still drop unidirectional or backpressured wss:// streams when writing frames to slow clients.
Mixed Content Blocking ws:// From an https:// Frontend
Modern browsers strictly block plain ws:// connections opened from an https:// page. Terminate TLS on port 443 in Nginx and connect from client code using wss://wss.example.com/ws, while keeping the internal Nginx-to-upstream link as fast http://127.0.0.1:8080.
Complete Your Deployment With Matching Docker Compose & CORS Tools
Pair this Nginx reverse proxy block with healthcheck-gated Docker Compose orchestration and verified browser CORS headers:
Generate a Docker Compose stack for Laravel Octane, PostgreSQL 16, Redis 7, and Laravel Reverb WebSockets with pg_isready healthchecks and firewall safety.
Resolve CORS preflight errors between Node.js Express APIs and React Vite on port 5173. Generate cors() configs, fix PATCH/DELETE methods, and debug headers.
Frequently Asked Questions
- Why does my wss:// reverse proxy in Nginx drop idle connections after 60 seconds?
- Nginx enforces a 60-second default for both proxy_read_timeout and proxy_send_timeout. Raise both directives (for example to 3600s) and send WebSocket ping/pong heartbeats every 25 to 30 seconds so intermediate CDNs or NAT firewalls also keep the connection open.
- Does Nginx proxy_pass need wss:// or http:// when terminating TLS for WebSockets?
- Use http://127.0.0.1:PORT in proxy_pass when Nginx terminates TLS on port 443 and forwards over local loopback. Nginx performs the HTTP/1.1 Upgrade: websocket handshake over plain HTTP internally while browsers connect securely over wss://.
- Why should I use map $http_upgrade $connection_upgrade instead of Connection "upgrade"?
- Hardcoding Connection "upgrade" sends an upgrade token even on standard HTTPS GET/POST requests that contain no Upgrade header. Using map sends Connection: upgrade strictly during WebSocket handshakes and close (or empty string for keepalive pools) for regular requests.
Related Nginx Reverse Proxy Guides
Browse all 13 Nginx guides →Nginx Reverse Proxy Config for Socket.IO & WebSockets (Upgrade & 3600s)
Fix WebSocket 400 errors and 60-second disconnects in Nginx for Socket.IO and ws servers. Generate Upgrade, Connection map, and 3600s proxy_read_timeout blocks.
Nginx Reverse Proxy for Odoo WebSockets (/websocket on Port 8072)
Fix Odoo 16/17+ WebSocket live chat disconnects in Nginx. Proxy /websocket to gevent port 8072 with Upgrade headers, 720s timeouts, and / to 8069.
Nginx Reverse Proxy Config for Laravel Octane & Reverb WebSockets
Configure Nginx reverse proxy for Laravel Octane (FrankenPHP/Swoole) on port 8000 and Laravel Reverb WebSockets on port 8080 with SSL and 3600s timeouts.