转到主要内容
Version: 3.x

Behind a reverse proxy

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

Content of /etc/nginx/nginx.conf:

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

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:

Apache HTTPD

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]

ProxyTimeout 3

Related:

Node.js 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

Content of Caddyfile for Caddy 2

example.com {
rewrite /path /path/
handle /path/* {
uri strip_prefix /path
rewrite * /socket.io{path}
reverse_proxy localhost:3000 {
header_up Host {host}
header_up X-Real-IP {remote}
}
}
}

Related