Nginx/HAProxy Load Balancing Setup for Website

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Load Balancing Setup with Nginx and HAProxy

Load balancer distributes incoming traffic across multiple backends, eliminates single point of failure, allows horizontal scaling without client code changes.

Nginx as load balancer

Nginx balances HTTP/HTTPS and TCP out of the box. Suitable for most web apps: proxies requests to backends, health checks, caches responses.

# /etc/nginx/conf.d/myapp.conf

upstream myapp_backend {
    # Round-robin by default
    server 10.0.1.10:8080;
    server 10.0.1.11:8080;
    server 10.0.1.12:8080;

    keepalive 32;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    location / {
        proxy_pass http://myapp_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_connect_timeout 5s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;

        proxy_next_upstream error timeout http_502 http_503;
        proxy_next_upstream_tries 2;
    }
}

Nginx balancing algorithms

# Least Connections
upstream backend_lc {
    least_conn;
    server 10.0.1.10:8080;
    server 10.0.1.11:8080;
}

# IP Hash
upstream backend_sticky {
    ip_hash;
    server 10.0.1.10:8080;
    server 10.0.1.11:8080;
}

# Weighted Round-Robin
upstream backend_weighted {
    server 10.0.1.10:8080 weight=3;
    server 10.0.1.11:8080 weight=1;
}

# Backup server
upstream backend_backup {
    server 10.0.1.10:8080;
    server 10.0.1.11:8080;
    server 10.0.1.20:8080 backup;
}

HAProxy for high loads

HAProxy is a specialized load balancer. Handles millions of concurrent connections, L4 (TCP) and L7 (HTTP), built-in stats and ACLs.

# /etc/haproxy/haproxy.cfg

global
    log /dev/log local0
    maxconn 100000
    nbthread 4
    tune.ssl.default-dh-param 2048

defaults
    log global
    mode http
    option httplog
    option forwardfor
    option http-server-close
    timeout connect 5s
    timeout client 30s
    timeout server 60s

frontend http_front
    bind *:80
    bind *:443 ssl crt /etc/haproxy/certs/example.com.pem

    http-request redirect scheme https unless { ssl_fc }

    acl is_api path_beg /api/
    acl is_admin path_beg /admin/

    use_backend api_backend if is_api
    use_backend admin_backend if is_admin
    default_backend web_backend

backend web_backend
    balance leastconn
    option httpchk GET /health HTTP/1.1\r\nHost:\ example.com

    server web01 10.0.1.10:8080 check
    server web02 10.0.1.11:8080 check
    server web03 10.0.1.12:8080 check

backend api_backend
    balance roundrobin
    option httpchk GET /api/health

    server api01 10.0.2.10:3000 check
    server api02 10.0.2.11:3000 check

frontend stats
    bind *:8404
    stats enable
    stats uri /stats
    stats auth admin:strongpassword

Keepalived for HA

Two load balancers (primary + backup) with virtual IP — if primary fails, backup takes VIP in seconds.

# /etc/keepalived/keepalived.conf (primary)
vrrp_script check_haproxy {
    script "pidof haproxy"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass secretpassword
    }

    virtual_ipaddress {
        10.0.0.100/24
    }

    track_script {
        check_haproxy
    }
}

Timeline

Task Timeline
Nginx balancing + SSL termination 1–2 days
HAProxy + stats + ACL 2–3 days
Keepalived HA pair +1 day
Dynamic upstream updates +1–2 days