数字海洋上的负载平衡Websocket

时间:2018-07-17 07:39:18

标签: tcp websocket load-balancing digital-ocean

我正在尝试配置Digital Ocean本机负载平衡器以分配WebSocket流量。我设置了规则:{{3}}

在尝试通过负载均衡器进行连接时,我得到: VM915:1 WebSocket connection to 'ws://{loadbalancerip}:8443/' failed: Connection closed before receiving a handshake response

直接连接就可以了。

那我该如何配置负载均衡器来平衡websocket的流量呢?

1 个答案:

答案 0 :(得分:0)

由于Digital Ocean Load Balancer似乎不支持开箱即用,我不得不购买一个小型实例并在其上进行配置 Nginx 以平衡3台本地计算机之间的传入流量

这里是 Nginx 的可能配置,它使您能够平衡wss从Cloudflare转发到8443端口的流量:

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;

    }
}