Compile Nginx with Load Balancing, Reverse Proxy, and Health Checks – A Step‑by‑Step Guide

This tutorial walks through compiling Nginx from source, configuring a three‑node backend pool with weighted load balancing, setting up reverse‑proxy directives, integrating the nginx_upstream_check_module for health monitoring, and verifying the setup with curl and test commands.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Compile Nginx with Load Balancing, Reverse Proxy, and Health Checks – A Step‑by‑Step Guide

1. Compile and Install Nginx

Install the required build tools and download the source package, then extract and configure the build with a custom prefix.

shell > yum -y install gcc gcc-c++ make wget zlib-devel pcre-devel openssl-devel
shell > wget http://nginx.org/download/nginx-1.12.2.tar.gz
shell > tar zxf nginx-1.12.2.tar.gz
shell > cd nginx-1.12.2
shell > ./configure --prefix=/usr/local/nginx-1.12.2 && make && make install

2. Prepare Backend Web Servers

Three Tomcat instances are started on ports 8080, 8081 and 8082. Use curl to verify each server returns its own welcome message.

shell > curl 192.168.10.24:8080
welcome to tomcat1
shell > curl 192.168.10.24:8081
welcome to tomcat2
shell > curl 192.168.10.24:8082
welcome to tomcat3

All three back‑ends are up and responding.

3. Nginx Load‑Balancing Configuration

The configuration relies on ngx_http_proxy_module and ngx_http_upstream_module . An upstream block named ls defines the three servers with different weights (1, 2, 3) and failure handling parameters.

weight – higher value receives more traffic

max_fails – number of failed attempts before the server is marked down

fail_timeout – period during which the server stays down

user  nobody;
worker_processes  1;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    upstream ls {
        server 192.168.10.24:8080 weight=1 max_fails=3 fail_timeout=20s;
        server 192.168.10.24:8081 weight=2 max_fails=3 fail_timeout=20s;
        server 192.168.10.24:8082 weight=3 max_fails=3 fail_timeout=20s;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://ls;
        }
    }
}

This minimal configuration creates a weighted round‑robin pool; requests to Nginx on port 80 are distributed according to the defined weights.

4. Reverse‑Proxy Settings

Additional proxy directives control error handling, header forwarding, time‑outs, and buffer sizes.

# Fail‑over strategy
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;

# Preserve client IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# Buffer and timeout tuning
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

5. Adding Health‑Check Capability

The upstream health‑check module is not part of the official Nginx distribution. Clone the module, apply patches that match the Nginx version, and rebuild Nginx with --add-module pointing to the module source.

shell > git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
shell > yum -y install patch
shell > cd /usr/local/src/nginx-1.12.2
shell > patch -p1 < /usr/local/src/nginx_upstream_check_module/check_1.12.1+.patch
shell > ./configure --prefix=/usr/local/nginx-1.12.2 \
    --add-module=/usr/local/src/nginx_upstream_check_module
shell > make && make install

Update the upstream block to enable health checks:

upstream ls {
    server 192.168.10.24:8080;
    server 192.168.10.24:8081;
    server 192.168.10.24:8082;
    check interval=3000 rise=2 fall=5 timeout=1000 type=http;
}

server {
    listen 80;
    location / {
        proxy_pass http://ls;
    }
    location /status {
        check_status;
        access_log off;
    }
}

The health‑check runs every 3 seconds, marks a server as up after two successful probes, and as down after five failures. The /status endpoint returns a JSON report of each node’s state.

6. Verification

Test the configuration syntax and start Nginx:

shell > /usr/local/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful

shell > /usr/local/nginx-1.12.2/sbin/nginx

Send ten requests to the load‑balanced address and observe that the server with the highest weight (port 8082) handles the most requests.

i=0; while [ $i -lt 10 ]; do curl localhost; let i++; done
# Output shows a distribution of "welcome to tomcat" messages weighted 1:2:3

Disable one backend, reload Nginx, and query /status?format=json to confirm the failed node is marked down and excluded from the pool.

curl localhost/status?format=json
{
  "servers": {
    "total": 3,
    "server": [
      {"name":"192.168.10.24:8080","status":"up",...},
      {"name":"192.168.10.24:8081","status":"down",...},
      {"name":"192.168.10.24:8082","status":"up",...}
    ]
  }
}

When the downed server is restored and passes the health check, it is automatically re‑added to the load‑balancing pool.

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.

Compilationreverse proxyupstream
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

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.