WebSocket Proxying

Beginner
5 min read
Updated: Nov 2026
3 examples
Upgrade

Definition

WebSockets start as HTTP, then switch protocols with an Upgrade handshake. Behind a reverse proxy those hop-by-hop headers are not forwarded automatically.

Nginx can tunnel the connection when the backend returns 101 Switching Protocols. You must pass Upgrade and Connection explicitly with proxy_set_header.

Config

Start with this shape. Adjust paths for your server.

Nginx
location /chat/ {
    proxy_pass http://backend;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

On older Nginx builds you may also need proxy_http_version 1.1;. Current docs note that line mainly for versions before 1.29.7.

Key Directives

NamePlain meaning
proxy_set_header UpgradeForward the client Upgrade header to the backend.
proxy_set_header ConnectionSend upgrade (or a mapped value) so the backend can switch protocols.
map $http_upgradeSet Connection to upgrade only when Upgrade is present.
proxy_read_timeoutIncrease if idle sockets close after the default 60s.

Examples

Example 1 — Forward Upgrade Headers

Minimum lines for a WebSocket path behind Nginx.

Nginx
location /chat/ {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

How It Works

  1. Upgrade tells the backend the client wants WebSocket.
  2. Connection upgrade completes the hop-by-hop requirement.
  3. Host still helps the app see the public hostname.

Example 2 — Map Connection from Upgrade

A map keeps normal HTTP requests from always sending Connection: upgrade.

Nginx
http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    server {
        location /chat/ {
            proxy_pass http://backend;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }
}

How It Works

  1. Empty Upgrade maps to close.
  2. Any other value maps to upgrade.
  3. Safer default for mixed HTTP + WS locations.

Example 3 — Avoid Idle Timeouts

By default Nginx may close a quiet proxied connection after 60 seconds.

Nginx
location /chat/ {
    proxy_pass http://backend;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 3600;
}

How It Works

  1. proxy_read_timeout extends how long Nginx waits for backend data.
  2. App pings also keep the socket alive.
  3. Match timeouts to your chat or game needs.

Frequently Asked Questions

Usually Upgrade/Connection were not set, the backend is down, or a timeout closed the idle tunnel.
No. Only for protocol upgrades such as WebSocket.
Yes. It is the same proxy_pass path with extra headers. See the reverse proxy guide first.
Switching Protocols — the backend accepted the Upgrade. Nginx then tunnels bytes both ways.
Yes. Configure HTTPS on Nginx and proxy to ws:// or http:// on localhost, or to a TLS backend if required.

Next: Command-line parameters

Keep going through the beginner Nginx path.

Command-line parameters →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful