Skip to main content
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.soLoadModule authn_core_module            modules/mod_authn_core.soLoadModule authz_host_module            modules/mod_authz_host.soLoadModule authz_groupfile_module       modules/mod_authz_groupfile.soLoadModule authz_user_module            modules/mod_authz_user.soLoadModule authz_core_module            modules/mod_authz_core.so
LoadModule headers_module               modules/mod_headers.soLoadModule lbmethod_byrequests_module   modules/mod_lbmethod_byrequests.soLoadModule proxy_module                 modules/mod_proxy.soLoadModule proxy_balancer_module        modules/mod_proxy_balancer.soLoadModule proxy_http_module            modules/mod_proxy_http.soLoadModule proxy_wstunnel_module        modules/mod_proxy_wstunnel.soLoadModule rewrite_module               modules/mod_rewrite.soLoadModule slotmem_shm_module           modules/mod_slotmem_shm.soLoadModule unixd_module                 modules/mod_unixd.so
User daemonGroup daemon
ProxyPass / http://localhost:3000/RewriteEngine onRewriteCond %{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