在Digital Ocean上负载均衡Websockets

10
我正在尝试配置数字海洋本地负载平衡器以分发Websockets流量。我设置了规则:enter image description here
在尝试通过负载平衡器连接时,我收到以下错误信息:VM915:1 WebSocket connection to 'ws://{loadbalancerip}:8443/' failed: Connection closed before receiving a handshake response
直接连接可以正常工作。
那么,我该如何配置负载平衡器来平衡Websockets流量呢?

1
你能够让它工作吗?我也遇到了同样的问题。 - Jerome Miranda
1个回答

3
据我所知,Digital Ocean Load Balancer似乎不支持websockets。因此,我不得不购买一个小实例并在上面配置Nginx来平衡三台本地机器之间的传入流量。
下面是可能适用于Nginx的配置文件,它可以让您平衡通过Cloudflare转发到8443端口的wss流量:
upstream wss {
# Clients with the same IP are redirected to the same backend
# ip_hash;
# Available backend servers
server 228.228.228.1:8443 max_fails=3 fail_timeout=30s;
server 228.228.228.2:8443 max_fails=3 fail_timeout=30s;
server 228.228.228.3:8443 max_fails=3 fail_timeout=30s;
}

server {
listen 8443 ssl default_server;
listen 443 ssl default_server;
listen [::]:8443 ssl default_server;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
underscores_in_headers on;

root /var/www/html;

index index.html index.htm index.nginx-debian.html;

server_name _;


    location / {
        # switch off logging
        access_log off;
        try_files $uri $uri/ =404;

        # redirect all HTTP traffic to wss
        proxy_pass https://wss;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass_request_headers      on; 
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header HTTP_CF_IPCOUNTRY $http_cf_ipcountry;

        # WebSocket support (nginx 1.4)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Path rewriting
        rewrite /wss/(.*) /$1 break;
        proxy_redirect off;

    }
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接