Implementation of HAProxy and NGINX Reverse Proxy for a High-Availability Web Server

Ensuring continuous availability for web services requires resilient architecture, precise configuration, and active health monitoring—far beyond simply “running a web server.” This article presents a formal, end-to-end approach to implementing a high-availability web tier using HAProxy as a load balancer in front of multiple NGINX reverse proxy nodes, including conceptual framing, a concise executive summary, and a complete step-by-step deployment guide.

Introduction to the architecture

High-availability web architectures commonly place a highly efficient load balancer in front of multiple reverse proxy/web server nodes to distribute traffic, detect failures, and maintain service continuity. HAProxy provides robust Layer 4 and Layer 7 load balancing with health checks and operational visibility, while NGINX offers advanced application-layer capabilities such as HTTP/2/3, caching, and security features to optimize modern application delivery. In this design, HAProxy accepts client traffic on port 80 (and optionally 443) and balances requests across two or more NGINX instances, each capable of serving content and proxying to upstream applications.

Executive summary

  • Objective: Implement a fault-tolerant web tier that maintains service availability during node failures and scales horizontally with demand.
  • Core components:
    • HAProxy: Front-end load balancer with health checks, statistics, and flexible policies.
    • NGINX: Reverse proxy/web server supporting modern protocols, security features (e.g., WAF options), and performance optimizations.
  • Outcomes:
    • Resilience: Automatic failover when a backend becomes unavailable through active health checks.
    • Performance: Reduced latency and improved delivery using NGINX’s optimized HTTP stack and protocol support.
    • Visibility: Live statistics and status pages from HAProxy enable operational insights, including session rates and server health states.

Prerequisites and reference topology

  • Environment: Ubuntu 24.04 LTS on virtual or physical hosts with one HAProxy node (e.g., 192.168.1.100) and at least two NGINX nodes (e.g., 192.168.1.101 and 192.168.1.102). NGINX deployment can be verified via its default “Welcome to nginx! Server 1 or Welcome to nginx! Server 2” page prior to custom configuration.
  • Design: HAProxy terminates client connections and distributes them (e.g., round robin) across NGINX backends. NGINX serves static content and/or proxies requests to upstream app services, leveraging modern protocols and security features to enhance delivery.
  • Operational visibility: HAProxy statistics expose version, uptime, session metrics, and per-backend health status, facilitating validation and tuning during rollouts.

Step-by-step implementation

Fontend load balancer (192.168.1.100)

Open terminal and type command below to install HAProxy

After installation successfully, navigate to /etc/haproxy$

Edit file haproxy.cfg like the code below

Validate and restart HAProxy

Configure NGINX on Backend Servers 1 (192.168.1.101)

Update and Install Required Packages

Edit the default server block to return a unique response per host:

Configure NGINX on Backend Servers 2 (192.168.1.102)

Update and Install Required Packages

After installation, confirm NGINX is active by visiting the server’s IP; the default welcome page indicates a successful setup and forms the baseline for validation.

Backend Servers 1 (192.168.1.101)

Backend Servers 2 (192.168.1.102)

Verify Load Balancing and Health Checks

1. Client Access

  • Open a web browser on the host computer or another virtual machine.
  • Navigate to the HAProxy IP address (for example, http://192.168.1.100).
  • If the configuration is correct, you will see a page served by one of the NGINX servers.

2. Load Balancing Test

Create distinct pages on each NGINX server, for example:

  • NGINX1 (192.168.1.101): display the text “Welcome to nginx! Server 1”
  • NGINX2 (192.168.1.102): display the text “Welcome to nginx! Server 2”
  • Refresh the web browser several times.
  • If load balancing is functioning correctly, the pages will alternately display “Welcome to nginx! Server 1” and “Welcome to nginx! Server 2.”

3. Health Check Test

Shut down one of the NGINX servers by running:

  • Access the HAProxy IP address again.
  • HAProxy will automatically redirect traffic to the NGINX server that remains active.
  • You will not encounter any errors; instead, you will only see the page served by the active server.

4. Monitoring HAProxy

Access http://192.168.1.100:8081/haproxy?stats to view the status of the backend servers.


Jasa System Administrator Profesional
Saya menawarkan layanan instalasi dan konfigurasi yang komprehensif untuk mendukung kebutuhan infrastruktur IT Anda, meliputi:

  • Web Server: instalasi dan optimasi server agar website berjalan stabil dan aman dengan menggunakan platform dari Apache & NGINX.
  • Mail Server: instalasi dan email server seperti Microsoft 365, Carbonio (Zimbra), Google Workspace.
  • Hosting Control Panel: pemasangan dan pengaturan panel hosting modern untuk memudahkan manajemen server.
  • Content Management System (CMS): instalasi WordPress, Joomla, atau platform lain untuk pengelolaan konten yang efisien.
  • Web Static Application : setup aplikasi berbasis web sesuai kebutuhan bisnis Anda.
  • Migrasi Server/Website: pemindahan data dan aplikasi dengan aman tanpa downtime.

📩 Hubungi saya untuk konsultasi dan pemesanan layanan.

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *