反向代理

您将在下面找到在反向代理解决方案后面部署 Socket.IO 服务器所需的配置,例如:

在多服务器设置中,请查看此处的文档。

NginX

/etc/nginx/nginx.conf内容:

  1. http {
  2. server {
  3. listen 80;
  4. server_name example.com;
  5. location / {
  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. }

有关的:

如果您只想转发 Socket.IO 请求(例如当 NginX 处理静态内容时):

  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. }

或使用自定义路径:

  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. }

在这种情况下,必须相应地配置服务器和客户端:

服务器

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

客户端

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

Apache HTTPD

/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

有关的:

Node.js http-proxy

安装: 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

Caddy 2Caddyfile的内容

  1. example.com {
  2. rewrite /path /path/
  3. handle /path/* {
  4. uri strip_prefix /path
  5. rewrite * /socket.io{path}
  6. reverse_proxy localhost:3000 {
  7. header_up Host {host}
  8. header_up X-Real-IP {remote}
  9. }
  10. }
  11. }

有关的: