2

I've been messing around with Apache as a load balancer for my Socket.io server. I went through the following topic and now everything seems to be fine.

Configuring Apache 2.4 mod_proxy_wstunnel for Socket.IO 1.0

I configured Apache using the following config and now the connections are being upgraded to WebSocket. But when I try to enable sticky sessions for the following config, it seems like sometimes apache routes the upgrade request to route #2 while the polling was established on route #1. When this happens, upgrade fails and websocket is not connected. Since we have to balancers (one for http and one for ws), I thought maybe the cookie set by http route #1 , cannot be accessed by ws routes and that's why sessions are not sticky among http and ws.

I'm using apache 2.4.9

<VirtualHost *:8080>
    Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

    ProxyRequests off

    ServerName localhost

    <Proxy balancer://http-localhost/>
        BalancerMember http://localhost:8081 route=1 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
        BalancerMember http://localhost:8082 route=2 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900

        ProxySet lbmethod=byrequests
        ProxySet stickysession=ROUTEID
    </Proxy>

    <Proxy balancer://ws-localhost/>
        BalancerMember ws://localhost:8081 route=1 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
        BalancerMember ws://localhost:8082 route=2 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900

        ProxySet lbmethod=byrequests
        ProxySet stickysession=ROUTEID
    </Proxy>

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/socket.io                [NC]
    RewriteCond %{QUERY_STRING} transport=websocket        [NC]
    RewriteRule /(.*)           balancer://ws-localhost/$1 [P,L]

    ProxyPass /socket.io balancer://http-localhost/socket.io
    ProxyPassReverse /socket.io balancer://http-localhost/socket.io
</VirtualHost>
MadHatter
  • 78,442
  • 20
  • 178
  • 229
Peyman
  • 21
  • 2

0 Answers0