Skip to main content
Version: 4.x

在反向代理后面

你将在下面找到在反向代理解决方案后面部署 Socket.IO 服务器所需的配置,例如:

¥You will find below the configuration needed for deploying a Socket.IO server behind a reverse-proxy solution, such as:

在多服务器设置中,请查看文档 此处

¥In a multi-server setup, please check the documentation here.

nginx

/etc/nginx/nginx.conf 的内容:

¥Content of /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 http://localhost:3000;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}

有关的:

¥Related:

caution

nginx 的 proxy_read_timeout(默认 60 秒)的值必须大于 Socket.IO 的 pingInterval + pingTimeout(默认 45 秒),否则如果在给定的延迟后没有数据发送,nginx 将强制关闭连接,客户端将收到 "传输关闭" 错误 。

¥The value of nginx's proxy_read_timeout (60 seconds by default) must be bigger than Socket.IO's pingInterval + pingTimeout (45 seconds by default), else nginx will forcefully close the connection if no data is sent after the given delay and the client will get a "transport close" error.

如果你只想转发 Socket.IO 请求(例如当 nginx 处理静态内容时):

¥If you only want to forward the Socket.IO requests (for example when nginx handles the static content):

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 http://localhost:3000;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}

或者使用自定义 路径

¥Or with a custom 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 http://localhost:3000;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}

在这种情况下,服务器和客户端必须进行相应配置:

¥In that case, the server and the client must be configured accordingly:

服务器

¥Server

import { Server } from "socket.io";

const io = new Server({
path: "/my-custom-path/"
});

客户端

¥Client

import { io } from "socket.io-client";

const socket = io({
path: "/my-custom-path/"
});

Apache HTTPD

/usr/local/apache2/conf/httpd.conf 的内容:

¥Content of /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 / http://localhost:3000/
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:3000/$1" [P,L]

# must be bigger than pingInterval (25s by default) + pingTimeout (20s by default)
ProxyTimeout 60

有关的:

¥Related:

Node.js http-proxy

安装:npm i http-proxy

¥Installation: npm i http-proxy

const httpProxy = require("http-proxy");

httpProxy
.createProxyServer({
target: "http://localhost:3000",
ws: true,
})
.listen(80);

文档

¥Documentation

Caddy 2

如果只想转发 Socket.IO 请求,则将 Caddyfile 的内容替换为 Caddy 2

¥Content of Caddyfile for Caddy 2, if you only want to forward the Socket.IO requests

example.com {
reverse_proxy /socket.io/* localhost:3000
}

或者,如果你想要自定义路径:

¥Or, if you want a custom path:

example.com {
rewrite /path /path/
handle_path /path/* {
rewrite * /socket.io{path}
reverse_proxy localhost:3000
}
}

有关的

¥Related