反向代理背后的 Socket.IO
您将在下面找到在反向代理解决方案(例如)后面部署 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 http://localhost: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 将在给定延迟后强制关闭连接,客户端将收到“传输关闭”错误。
如果您只想转发 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 http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
或者使用自定义 路径
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";
}
}
}
在这种情况下,服务器和客户端必须相应地配置
服务器
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 / 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
相关
Node.js http-proxy
安装:npm i http-proxy
const httpProxy = require("http-proxy");
httpProxy
.createProxyServer({
target: "http://localhost: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
}
}
相关