跳到主要內容
版本: 4.x

反向代理的背後

您將在下方找到在反向代理解決方案(例如

在多伺服器設定中,請在此處查看文件 在此

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 會在指定延遲後強制關閉連線,而用戶端會收到「傳輸關閉」錯誤。

如果你只想轉發 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";
}
}
}

或使用自訂 路徑

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 2Caddyfile 內容

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

或者,如果你想要自訂路徑

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

相關