apache mod代理平衡器与websockets

时间:2017-05-09 19:12:54

标签: apache websocket uwsgi

我有uWSGI + Apache以及以下apache配置:

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*)           ws://127.0.0.1:3031/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*)           http://127.0.0.1:3031/$1 [P,L]

ProxyPass / http://127.0.0.1:3031/

现在我想实现粘性会话并让apache为每个worker使用代理平衡器:

<Proxy "balancer://uwsgiworkers">
    BalancerMember "http://127.0.0.1:3031" route=1
    BalancerMember "http://127.0.0.1:3032" route=2
    BalancerMember "http://127.0.0.1:3033" route=3
    ProxySet stickysession=ROUTEID
    ProxySet lbmethod=byrequests
</Proxy> 

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*)           ws://??????/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*)           http://???????/$1 [P,L]

ProxyPass balancer://uwsgiworkers/

现在问题是如何传递给RewriteRule选定的工作人员网址?因为我需要在WebSockets握手中更改协议。

谢谢!

1 个答案:

答案 0 :(得分:0)

我最近偶然发现了同样的问题。我不是100%肯定这是正确的选择(感觉很冗长),但这似乎是一个功能选择!

基本上,我创建了两个不同的平衡器组,一个用于HTTP通信,另一个用于WebSocket。我不太确定是否可以在这里省略ProxyPass(似乎balancer://处理了ProxyPass部分),但是在我的环境中似乎工作得很好!

<Proxy "balancer://uwsgiworkers">
    BalancerMember "http://127.0.0.1:3031" route=1
    BalancerMember "http://127.0.0.1:3032" route=2
    BalancerMember "http://127.0.0.1:3033" route=3
    ProxySet stickysession=ROUTEID
    ProxySet lbmethod=byrequests
</Proxy> 

<Proxy "balancer://uwsgiworkersws">
    BalancerMember "ws://127.0.0.1:3031" route=1
    BalancerMember "ws://127.0.0.1:3032" route=2
    BalancerMember "ws://127.0.0.1:3033" route=3
    ProxySet stickysession=ROUTEID
    ProxySet lbmethod=byrequests
</Proxy> 

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*)           balancer://uswsgiworkersws/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*)           balancer://uswsgiworkers/$1 [P,L]

# I actually think this can be omitted
# it seems that balancer:// handles the ProxyPass
# ProxyPass balancer://uwsgiworkers/

该想法的信用:https://serverfault.com/a/808640/437243