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:

  1. http {
  2. server {
  3. listen 80;
  4. location / {
  5. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  6. proxy_set_header Host $host;
  7. proxy_pass http://localhost:3000;
  8. proxy_http_version 1.1;
  9. proxy_set_header Upgrade $http_upgrade;
  10. proxy_set_header Connection "upgrade";
  11. }
  12. }
  13. }

Related:

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

  1. http {
  2. server {
  3. listen 80;
  4. root /var/www/html;
  5. location /socket.io/ {
  6. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  7. proxy_set_header Host $host;
  8. proxy_pass http://localhost:3000;
  9. proxy_http_version 1.1;
  10. proxy_set_header Upgrade $http_upgrade;
  11. proxy_set_header Connection "upgrade";
  12. }
  13. }
  14. }

Or with a custom path:

  1. http {
  2. server {
  3. listen 80;
  4. root /var/www/html;
  5. location /my-custom-path/ {
  6. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  7. proxy_set_header Host $host;
  8. proxy_pass http://localhost:3000;
  9. proxy_http_version 1.1;
  10. proxy_set_header Upgrade $http_upgrade;
  11. proxy_set_header Connection "upgrade";
  12. }
  13. }
  14. }

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

Server

  1. import { Server } from "socket.io";
  2. const io = new Server({
  3. path: "/my-custom-path/"
  4. });

Client

  1. import { io } from "socket.io-client";
  2. const socket = io({
  3. path: "/my-custom-path/"
  4. });

Apache HTTPD

Content of /usr/local/apache2/conf/httpd.conf:

  1. Listen 80
  2. ServerName example.com
  3. LoadModule mpm_event_module modules/mod_mpm_event.so
  4. LoadModule authn_file_module modules/mod_authn_file.so
  5. LoadModule authn_core_module modules/mod_authn_core.so
  6. LoadModule authz_host_module modules/mod_authz_host.so
  7. LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
  8. LoadModule authz_user_module modules/mod_authz_user.so
  9. LoadModule authz_core_module modules/mod_authz_core.so
  10. LoadModule headers_module modules/mod_headers.so
  11. LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
  12. LoadModule proxy_module modules/mod_proxy.so
  13. LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
  14. LoadModule proxy_http_module modules/mod_proxy_http.so
  15. LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
  16. LoadModule rewrite_module modules/mod_rewrite.so
  17. LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
  18. LoadModule unixd_module modules/mod_unixd.so
  19. User daemon
  20. Group daemon
  21. ProxyPass / http://localhost:3000/
  22. RewriteEngine on
  23. RewriteCond %{HTTP:Upgrade} websocket [NC]
  24. RewriteCond %{HTTP:Connection} upgrade [NC]
  25. RewriteRule ^/?(.*) "ws://localhost:3000/$1" [P,L]
  26. ProxyTimeout 3

Related:

Node.js http-proxy

Installation: npm i http-proxy

  1. const httpProxy = require("http-proxy");
  2. httpProxy
  3. .createProxyServer({
  4. target: "http://localhost:3000",
  5. ws: true,
  6. })
  7. .listen(80);

Documentation

Caddy 2

Content of Caddyfile for Caddy 2

  1. example.com {
  2. rewrite /path /path/
  3. handle_path /path/* {
  4. rewrite * /socket.io{path}
  5. reverse_proxy localhost:3000
  6. }
  7. }

Related