Nginx Reverse Proxy Builder · TLS / WSS WebSockets

Nginx WSS Reverse Proxy & $connection_upgrade Map Config (TLS 443)

Generate an Nginx reverse proxy server block for TLS / WSS WebSockets with URL prefix rewrite previews, Certbot SSL redirects, WebSocket upgrade headers, and Docker DNS checks.

Written & verified by Chathura Prabashana · Updated November 2026

1. Adjust stack settings or load a test scenario2. Inspect live diagnostics3. Copy generated config
Quick-Test Scenarios:

1. Domain, Routing & Upstream

2. TLS, WebSockets & Directives

# /etc/nginx/sites-available/wss.example.com.conf
# Generated by OpsCheck DevTools — Validate with: sudo nginx -t && sudo systemctl reload nginx

# Required in http {} context (or top of sites-available file) for clean WebSocket upgrades
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name wss.example.com;

    # Redirect all plaintext HTTP traffic permanently to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name wss.example.com;

    # Let's Encrypt Certbot TLS Certificates
    ssl_certificate     /etc/letsencrypt/live/wss.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/wss.example.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    client_max_body_size 10M;

    # Baseline HTTP Security Headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    location /ws {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        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_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;

        # WebSocket & Upgrade support (requires map $http_upgrade $connection_upgrade)
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_cache_bypass $http_upgrade;

        proxy_connect_timeout 15s;
        proxy_send_timeout 3600s;
        proxy_read_timeout 3600s;
    }
}
$sudo nginx -t && sudo systemctl reload nginx
URL Routing TransformationPrefix Preserved
GET https://wss.example.com/ws/usershttp://127.0.0.1:8080/ws/users

Strip Prefix OFF: Nginx preserves the full URI so upstream receives '/ws/users'.

Configuration Diagnostics

  • infoPrefix Preserved (/ws Forwarded Intact)

    GET https://wss.example.com/ws/users -> Upstream receives http://127.0.0.1:8080/ws/users. Strip Prefix OFF: Nginx preserves the full URI so upstream receives '/ws/users'.

  • passWebSocket Upgrade & Extended Timeout (3600s) Enabled

    Configured HTTP/1.1 Upgrade, Connection $connection_upgrade map, and extended proxy_read_timeout for persistent real-time sockets.

  • infoSingle-Layer CORS Policy Reminder

    This configuration intentionally omits Access-Control-Allow-Origin headers so your upstream backend framework (Laravel, FastAPI, Express, ASP.NET Core) owns CORS cleanly without duplicate header rejections.

Runs locally in your browser. Domain names and upstream topology are never sent to a server.

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:

/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.

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.

  1. 1Broken Unconditional Upgrade vs. Hardened TLS 443 + $connection_upgrade Map

    /etc/nginx/sites-available/wss.example.com.conf

    Place 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;
        }
    }
  2. 2Use Empty String Instead of close When Combining WSS With Upstream Keepalive

    /etc/nginx/conf.d/websocket-map.conf

    If 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.confnginx
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      "";
    }
    
    upstream realtime_backend {
        server 127.0.0.1:8080;
        keepalive 32;
    }
  3. 3Test TLS 443 WSS Handshake (HTTP 101) and Regular HTTPS Requests Together

    terminal

    Reload 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.

    terminalbash
    sudo 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.

Pitfall 1

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/.

Pitfall 2

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.

Pitfall 3

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.

Related Tools

Pair this Nginx reverse proxy block with healthcheck-gated Docker Compose orchestration and verified browser CORS 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.
Browse all 13 Nginx guides →