NGINX as Load Balancer

Alejandro15 days ago

Hi all,

I’ve deployed a horizontally scaled Traccar setup with two VPS instances using multicast. Now, I’m looking to add another VPS to handle load balancing with NGINX, but I haven’t been able to find clear documentation on this, either here in the forums or elsewhere online.

This setup has some complexities: it needs to route HTTP/HTTPS traffic, WebSocket traffic on port 8082, and GPS device traffic on multiple ports. Configuring NGINX to handle this properly isn’t straightforward.

I’d really appreciate it if anyone who has set up something similar could share their NGINX configuration files or point me to any resources with more information.

Thanks in advance!

odhiambo15 days ago

"Configuring Nginx to handle this properly isn't straightforward." That's because Nginx is not natively meant for that.
If you need a load balancer, use one that fits the requirement. https://www.haproxy.org/ - whose definition seems to match what you want.

Track-trace15 days ago

Are these kind of tutorials from Google search helpfull ?

https://codedamn.com/news/backend/horizontal-scaling-clustering-solutions

Alejandro15 days ago

Hi again,

Thank you, @dhiambo, for your suggestion! I am currently in the process of setting up HAProxy as a load balancer for my Traccar servers. After researching and adjusting the configuration, I’m moving forward with an initial setup that I’ll share here to get feedback.

My main concerns are around handling WebSocket traffic effectively, given that this protocol requires sticky sessions for proper operation. I would be grateful if anyone could take a look at the configuration and point out any potential issues or suggest improvements, especially related to WebSocket handling.

Thanks so much for your help and insights!

# HAProxy Configuration for Load Balancing
 defaults
    mode http
    timeout client 900s #to prevent disconnect traccar device. Same as traccar device timeout
    timeout connect 5s
    timeout server 900s #to prevent disconnect traccar device. Same as traccar device timeout
    log /dev/log local0 notice

# FRONTENDS 
    
# Frontend for HTTP
frontend http_front
    bind *:80
    bind *:8082
    use_backend http_back if { path_beg /ws }
    default_backend http_back

# Frontend for HTTPS
frontend https_front
    bind *:443 ssl crt /etc/letsencrypt/live/myserver.com/fullchain.pem #execute first: ln -s /etc/letsencrypt/live/myserver/privkey.pem /etc/letsencrypt/live/myserver.com/fullchain.pem.key 
    default_backend https_back
    
# Frontend for GPS Devices (Port 5023 gt06)
frontend gps_front_5023
    bind *:5023
    mode tcp
    default_backend gps_back_5023
    
# BACKENDS    

# Backend for HTTP (Port 80)
backend http_back
    balance roundrobin
    stick-table type ip size 1m expire 60m
    stick on src
    server http_server1 10.0.0.4:8082 check
    server http_server2 10.0.0.2:8082 check

# Backend for HTTPS (Port 443)
backend https_back
    balance roundrobin
    stick-table type ip size 1m expire 60m
    stick on src
    server https_server1 10.0.0.4:8082 check
    server https_server2 10.0.0.2:8082 check


# Backend for GPS Devices (Port 5023 gt06)
backend gps_back_5023
    mode tcp
    balance leastconn
    stick-table type ip size 1m expire 30m
    stick on src
    option tcp-check
    server gps_server1_5023 10.0.0.4:5023 check
    server gps_server2_5023 10.0.0.2:5023 check