0

I have an Nginx instance which serves requests proxied to it from an haproxy. This instance should balance load between several websocket server. To get better performance the load balancing should be sticky and so i used ip_hash as the load balancing method which not works as all requests are coming from haproxy. so i tried to load balance based on x-forwarded-for header like:

    upstream orderbook {
    hash $http_x_forwarded_for consistent;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
}

using this method distributed load between backends but ruins session persistence (i guess so because most of the clients switch from websockets to longpolling)! What can i do to achieve LB based on real client ips?

EDIT: After trying everything most of clients switched to using websocket again by changing client connection configurations and adding:

transports: ['websocket','polling'],

It seems that in the default configuration socket.io first tries to make a long polling connection. But I wonder what was the link between using nginx load balancing and change in transport of choice by clients.

4
  • Please be more specific about "not working". How did you determine that this is the case? Commented Jul 5, 2022 at 8:59
  • @GeraldSchneider Edited the post and clarified the problem. Thanks for your comment.
    – mhk
    Commented Jul 5, 2022 at 10:12
  • You didn't answer the question though. How many client IPs did you use for testing? If you only used a small number chances are high that the calculated hash places them on the same backend server. But that would not be a good test. Commented Jul 5, 2022 at 10:49
  • @GeraldSchneider It is a production server and there are 2.2k open websocket connections.
    – mhk
    Commented Jul 5, 2022 at 15:47

1 Answer 1

0

To ensure that the WebSocket connections are sticky and always routed to the same backend node, you can use the ip_hash directive in Nginx. This directive enables session persistence based on the client's IP address.

Here's an example configuration that demonstrates how to use ip_hash for WebSocket load balancing:

http {
upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
    # Add more backend servers here if needed
}

server {
    listen 80;

    location /socket.io {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

}

3
  • Is there any other stronger persistence techniques compatible with websocket? For example using cookies? I think that the ip cannot be constant during user connection for example while moving from lte to wifi connection in a mobile device, in this scenario the websocket may be closed by the balancer.
    – Tobia
    Commented Jun 12 at 20:40
  • In this case, the client's TCP connection is also dropped and should be re-established, resulting in a new ip_hash combination. Commented Jun 15 at 10:53
  • Is it the same for sticky connection made by cookie?
    – Tobia
    Commented Jun 16 at 11:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .