简体   繁体   中英

apache mod proxy balancer with websockets

I have uWSGI + Apache with following apache config:

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/

Now I want to implement sticky sessions and make apache use proxy balancer for each 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/

And now problem is how to pass to RewriteRule selected worker URL? As I need to change protocol in WebSockets handshake.

Thanks!

I have just recently stumbled upon the same problem. I am not 100% certain this is the right path to go down (and it feels pretty verbose), but it seems to be a functional option!

Basically, I created two different balancer groups, one for HTTP traffic and one for WebSocket. I'm not quite sure whether ProxyPass can be omitted here (it seems that balancer:// takes care of the ProxyPass part), but it seems to work nicely in my environment!

<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/

Credit for the idea: https://serverfault.com/a/808640/437243

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM