Configure Nginx for Socket.IO

When deploying Socket.IO behind Nginx, the reverse proxy must forward HTTP requests and WebSocket upgrade headers.

The proxy must support:

  • HTTP request forwarding.
  • Connection upgrade headers.
  • Appropriate proxy timeouts.
  • Correct backend host and port.

Create or update your Nginx server configuration.

The following example assumes the Node.js server listens on localhost port 3000.

Proxy Timeout

Proxy idle timeouts must be compatible with Socket.IO heartbeat settings and any infrastructure-level connection limits.

Nginx Socket.IO Configuration
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 80;
    server_name api.example.com;

    location /socket.io/ {
        proxy_pass http://127.0.0.1:3000/socket.io/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_read_timeout 75s;
        proxy_send_timeout 75s;
    }

    location / {
        proxy_pass http://127.0.0.1:3000;
        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;
    }
}

Forwards normal HTTP requests and Socket.IO traffic to the Node.js backend while supporting WebSocket protocol upgrades.

Key Takeaways
  • Reverse proxies must support WebSocket upgrades.

  • Forward the required upgrade headers.

  • Configure proxy timeouts according to heartbeat behavior.