NewCode CLOUD — 20% off your first month on all cloud VPSClaim

How to Set Up Nginx Load Balancing on Hong Kong VPS: Scale Beyond One Server (2026)

· 6 min read

When a single server can no longer handle your application’s traffic — whether due to CPU saturation, RAM exhaustion, or the need for zero-downtime deployments — Nginx load balancing distributes requests across multiple backend servers. Running an Nginx load balancer on a Hong Kong VPS with CN2 GIA routing lets you scale horizontally while maintaining the low-latency China connectivity your users depend on.

This guide covers Nginx upstream configuration, load balancing algorithms, health checks, sticky sessions, and SSL termination — the full production configuration for a multi-server Asia-Pacific deployment.


Load Balancing Architecture

The standard architecture uses a dedicated Nginx reverse proxy as the entry point, distributing traffic to a pool of application servers (called “upstream” servers):

Internet
    ↓
[Hong Kong VPS — Nginx Load Balancer]  ← CN2 GIA routing, SSL termination
    ↓           ↓           ↓
[App Server 1] [App Server 2] [App Server 3]
    ↓
[Shared Database — PostgreSQL / MySQL]

The load balancer handles: SSL termination, CN2 GIA-optimised inbound connections, health checking of app servers, and traffic distribution. Application servers handle business logic without SSL overhead. The database is shared across all app servers.


Step 1: Basic Upstream Configuration

Install Nginx on your load balancer VPS:

apt update && apt install nginx -y

Create the load balancer configuration at /etc/nginx/sites-available/loadbalancer:

upstream app_servers {
    # Default: round-robin (requests distributed evenly in sequence)
    server 10.0.0.10:8080;
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
}

server {
    listen 80;
    server_name app.yourdomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name app.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/app.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.yourdomain.com/privkey.pem;

    location / {
        proxy_pass http://app_servers;
        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_http_version 1.1;
        proxy_set_header Connection "";
        proxy_read_timeout 60s;
        proxy_connect_timeout 10s;
    }
}
ln -s /etc/nginx/sites-available/loadbalancer /etc/nginx/sites-enabled/
certbot --nginx -d app.yourdomain.com
nginx -t && systemctl reload nginx

Step 2: Load Balancing Algorithms

Round Robin (default)

upstream app_servers {
    server 10.0.0.10:8080;
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
    # Requests: 1→server1, 2→server2, 3→server3, 4→server1...
}

Best for: stateless applications where all servers have equal capacity and sessions are stored in a shared cache (Redis).

Least Connections

upstream app_servers {
    least_conn;
    server 10.0.0.10:8080;
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
    # Sends each new request to the server with fewest active connections
}

Best for: applications with variable request processing times — long-running API calls or file uploads don’t starve other servers.

IP Hash (Sticky Sessions)

upstream app_servers {
    ip_hash;
    server 10.0.0.10:8080;
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
    # Same client IP always routes to the same server
}

Best for: applications that store session state in server memory rather than a shared cache — users always hit the server that holds their session.

Weighted Distribution

upstream app_servers {
    server 10.0.0.10:8080 weight=3;  # Receives 3x the traffic
    server 10.0.0.11:8080 weight=2;  # Receives 2x the traffic
    server 10.0.0.12:8080 weight=1;  # Receives 1x the traffic
    # Use when servers have different hardware capacity
}

Best for: mixed-hardware server pools where a more powerful server should handle proportionally more traffic.


Step 3: Health Checks

Nginx Open Source includes passive health checking — it marks a server as unavailable when it fails to respond. Configure sensitivity:

upstream app_servers {
    least_conn;

    server 10.0.0.10:8080 max_fails=3 fail_timeout=30s;
    server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;
    server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
    # After 3 consecutive failures within 30s, remove server for 30s
}

Backup Server

upstream app_servers {
    server 10.0.0.10:8080;
    server 10.0.0.11:8080;
    server 10.0.0.12:8080 backup;
    # Server 3 only receives traffic if servers 1 and 2 are both down
}

Active Health Checks (Nginx Plus / Open Source workaround)

Nginx Plus provides active health checks. For the open-source version, implement a health check endpoint in your application and use a monitoring tool (Prometheus blackbox exporter) to verify backend health independently from Nginx’s passive detection.

# Application health endpoint (Node.js example):
app.get('/health', (req, res) => {
  // Check database connectivity
  db.query('SELECT 1').then(() => {
    res.status(200).json({ status: 'healthy' });
  }).catch(() => {
    res.status(503).json({ status: 'unhealthy' });
  });
});

Step 4: Optimise Upstream Connections

upstream app_servers {
    least_conn;
    keepalive 32;  # Keep 32 idle connections open to upstream servers
    server 10.0.0.10:8080;
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
}

server {
    # ...
    location / {
        proxy_pass http://app_servers;
        proxy_http_version 1.1;
        proxy_set_header Connection "";  # Required for keepalive
        # ...
    }
}

Upstream keepalive connections eliminate TCP handshake overhead on every proxied request — particularly valuable when load balancer and app servers are on the same private network and connection establishment is fast but still adds latency.


Step 5: SSL Configuration for Load Balancer

server {
    listen 443 ssl http2;
    server_name app.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/app.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.yourdomain.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 1.1.1.1 8.8.8.8 valid=300s;
    resolver_timeout 5s;

    add_header Strict-Transport-Security "max-age=63072000" always;
}

SSL terminates at the load balancer. Backend communication between load balancer and app servers uses HTTP over the private network — no SSL overhead on internal hops.


Step 6: Rate Limiting on the Load Balancer

<code">http {
    # Rate limit zones
    limit_req_zone $binary_remote_addr zone=general:10m rate=30r/s;
    limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
    limit_req_zone $binary_remote_addr zone=login:10m rate=1r/m;

    server {
        location / {
            limit_req zone=general burst=50 nodelay;
            proxy_pass http://app_servers;
        }

        location /api/ {
            limit_req zone=api burst=20 nodelay;
            proxy_pass http://app_servers;
        }

        location /auth/login {
            limit_req zone=login burst=5 nodelay;
            proxy_pass http://app_servers;
        }
    }
}

Applying rate limits at the load balancer protects all app servers simultaneously — a single configuration point versus configuring rate limiting separately on each backend.


Step 7: Monitoring Load Balancer Performance

Enable Nginx Status Page

<code">location /nginx-status {
    stub_status;
    allow 127.0.0.1;
    deny all;
}
<code"># Check active connections and request rate
curl http://127.0.0.1/nginx-status

# Output:
# Active connections: 145
# server accepts handled requests
#  12847 12847 89234
# Reading: 3 Writing: 28 Waiting: 114

Log Upstream Response Times

log_format upstream_time '$remote_addr - $remote_user [$time_local] '
  '"$request" $status $body_bytes_sent '
  '"$http_referer" "$http_user_agent" '
  'rt=$request_time uct=$upstream_connect_time '
  'uht=$upstream_header_time urt=$upstream_response_time '
  'us=$upstream_addr';

access_log /var/log/nginx/upstream.log upstream_time;

The urt (upstream response time) field reveals which backend servers are slow — essential for identifying overloaded or failing application servers before they cause user-visible errors.


Step 8: Zero-Downtime Deployments with Load Balancing

With a load balancer in place, you can deploy application updates without downtime:

<code">#!/bin/bash
# Rolling deploy script — update one server at a time

APP_SERVERS=("10.0.0.10" "10.0.0.11" "10.0.0.12")

for SERVER in "${APP_SERVERS[@]}"; do
  echo "Deploying to $SERVER..."

  # Remove server from load balancer temporarily
  # (modify upstream config to add 'down' flag and reload)
  ssh root@$SERVER "
    cd /var/www/app
    git pull origin main
    npm install --production
    pm2 restart app
  "

  # Wait for server to pass health checks
  sleep 10
  curl -sf http://$SERVER:8080/health || { echo "Health check failed!"; exit 1; }

  echo "$SERVER deployed successfully"
done

echo "Rolling deploy complete — all servers updated"

Conclusion

Nginx load balancing on a Hong Kong VPS scales your application horizontally while maintaining CN2 GIA routing for mainland Chinese users at the entry point. The load balancer becomes the single CN2 GIA-connected server, distributing work to application servers on a private network — a clean architectural separation that lets you scale app servers independently of the network-optimised entry point.

The progression from single VPS → load balancer + multiple app servers is the standard horizontal scaling path for Asia-Pacific applications as they grow beyond single-server capacity.

Build your scalable architecture: Browse Server.HK Hong Kong VPS plans — start with a single VPS and add load balancing when traffic demands it.

Related articles

More in VPS & ServersServer setup guides