リバースプロキシの背後
以下に、nginx、Apache HTTPD、Node.jsの`http-proxy`、Caddy 2などのリバースプロキシソリューションの背後にSocket.IOサーバーをデプロイするために必要な設定を示します。
マルチサーバー設定では、こちらのドキュメントを確認してください。
nginx
/etc/nginx/nginx.confの内容
http {
  server {
    listen 80;
    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_pass https://:3000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }
}
関連情報
注意
nginxのproxy_read_timeout(デフォルトで60秒)の値は、Socket.IOのpingInterval + pingTimeout(デフォルトで45秒)よりも大きくする必要があります。そうでない場合、指定された遅延後にデータが送信されないと、nginxは接続を強制的に閉じ、クライアントは「transport close」エラーを受け取ります。
Socket.IOのリクエストのみ転送する場合(例: nginxが静的コンテンツを処理する場合)
http {
  server {
    listen 80;
    root /var/www/html;
    location /socket.io/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_pass https://:3000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }
}
または、カスタムpathを使用する場合
http {
  server {
    listen 80;
    root /var/www/html;
    location /my-custom-path/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_pass https://:3000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }
}
その場合、サーバーとクライアントを適切に設定する必要があります。
サーバー
import { Server } from "socket.io";
const io = new Server({
  path: "/my-custom-path/"
});
クライアント
import { io } from "socket.io-client";
const socket = io({
  path: "/my-custom-path/"
});
Apache HTTPD
/usr/local/apache2/conf/httpd.confの内容
Listen 80
ServerName example.com
LoadModule mpm_event_module             modules/mod_mpm_event.so
LoadModule authn_file_module            modules/mod_authn_file.so
LoadModule authn_core_module            modules/mod_authn_core.so
LoadModule authz_host_module            modules/mod_authz_host.so
LoadModule authz_groupfile_module       modules/mod_authz_groupfile.so
LoadModule authz_user_module            modules/mod_authz_user.so
LoadModule authz_core_module            modules/mod_authz_core.so
LoadModule headers_module               modules/mod_headers.so
LoadModule lbmethod_byrequests_module   modules/mod_lbmethod_byrequests.so
LoadModule proxy_module                 modules/mod_proxy.so
LoadModule proxy_balancer_module        modules/mod_proxy_balancer.so
LoadModule proxy_http_module            modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module        modules/mod_proxy_wstunnel.so
LoadModule rewrite_module               modules/mod_rewrite.so
LoadModule slotmem_shm_module           modules/mod_slotmem_shm.so
LoadModule unixd_module                 modules/mod_unixd.so
User daemon
Group daemon
ProxyPass / https://:3000/
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://:3000/$1" [P,L]
# must be bigger than pingInterval (25s by default) + pingTimeout (20s by default)
ProxyTimeout 60
関連情報
Node.js http-proxy
インストール: npm i http-proxy
const httpProxy = require("http-proxy");
httpProxy
  .createProxyServer({
    target: "https://:3000",
    ws: true,
  })
  .listen(80);
Caddy 2
Socket.IOのリクエストのみ転送する場合のCaddy 2のCaddyfileの内容
example.com {
    reverse_proxy /socket.io/* localhost:3000
}
または、カスタムパスを使用する場合
example.com {
  rewrite /path /path/
  handle_path /path/* {
    rewrite * /socket.io{path}
    reverse_proxy localhost:3000
  }
}
関連情報