Operations 3 min read

How to Set Up HAProxy Load Balancing for Two Nginx Servers

This guide walks you through installing HAProxy, configuring it to balance HTTP traffic between two Nginx instances on ports 8080 and 8081, and verifying the setup by repeatedly accessing a test page that alternates between the two backend servers.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Set Up HAProxy Load Balancing for Two Nginx Servers

Prerequisites

1. Install HAProxy.

2. Start two web application servers, here two Nginx instances.

Nginx1 on port 8080, Nginx2 on port 8081. Both serve a test.html page with different content (1 on nginx1, 2 on nginx2).

Goal

Configure HAProxy as a front‑end load balancer so that accessing test.html through HAProxy dynamically routes to nginx1 or nginx2, displaying 1 or 2. Refreshing the page multiple times should show alternating outputs.

Configuration File

Create a conf directory under HAProxy’s installation path and add haproxy.cfg with the following content:

global
    daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:7000
    default_backend neo4j

backend neo4j
    server s1 127.0.0.1:8080 maxconn 32
    server s2 127.0.0.1:8081 maxconn 32

listen admin
    bind *:7080
    stats enable

Explanation:

frontend http‑in : listens on port 7000.

server s1 and server s2 : define the two backend servers and their ports.

Start HAProxy with:

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

Testing

Visit http://<em>server_address</em>:7000/test.html. Refresh the page repeatedly; the output should alternate between 1 and 2, confirming that HAProxy is correctly distributing requests.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendload balancingConfigurationNginxHAProxy
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.