Operations 6 min read

How to Configure Nginx Load Balancing with Multiple Tomcat Instances on Windows

This step‑by‑step guide shows how to prepare two Tomcat servers, create a simple web project, configure Nginx as a reverse‑proxy load balancer with various strategies, start the services on Windows, and verify that requests are distributed across the Tomcat instances.

ITPUB
ITPUB
ITPUB
How to Configure Nginx Load Balancing with Multiple Tomcat Instances on Windows

Hello World

First prepare the environment:

Download two unpacked Tomcat instances and run them simultaneously (see the related article for details).

Download the binary package of Nginx from the official site and unpack it.

Create a simple web project that displays a marker (8081 or 8082) to identify which Tomcat serves the request.

Deploy the web project to each Tomcat instance as shown in the screenshots.

Configure Nginx

Edit nginx.conf located in the nginx-1.10.1/conf directory:

Add an upstream block inside the http{} section to define the server group (do not use localhost to avoid slow access):

upstream nginxDemo {
    server 127.0.0.1:8081;   # server 1
    server 127.0.0.1:8082;   # server 2
}

Change the listening port from 80 to 8080:

server {
    listen       8080;
    ...
}

In the location / block, set up reverse proxy to the upstream group:

location / {
    root   html;
    index  index.html index.htm;
    proxy_pass http://nginxDemo;  # forward to upstream
}

Start Nginx and Tomcat, Then Test

On Windows, double‑click nginx.exe inside the nginx-1.10.1 folder to start Nginx. Verify the process in Task Manager (screenshot below).

Open a browser and navigate to http://localhost:8080/nginxDemo/index.jsp. Each refresh will route the request to a different Tomcat instance, demonstrating round‑robin load balancing.

Nginx Load‑Balancing Strategies

Round‑robin (default) : Requests are distributed sequentially; failed servers are automatically removed.

Least connections : Sends the request to the server with the fewest active connections.

upstream nginxDemo {
    least_conn;
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;
}

Weight : Assigns a weight to each server; higher weight receives proportionally more requests.

upstream nginxDemo {
    server 127.0.0.1:8081 weight=2; # server A
    server 127.0.0.1:8082;          # server B
}

ip_hash : Routes requests from the same client IP to the same backend, useful for session persistence.

upstream nginxDemo {
    ip_hash;
    server 127.0.0.1:8081 weight=2; # server A
    server 127.0.0.1:8082;          # server B
}

url_hash (third‑party module) : Distributes based on a hash of the request URL; requires patching Nginx.

upstream nginxDemo {
    server 127.0.0.1:8081; # server A
    server 127.0.0.1:8082; # server B
    hash $request_url;
}

fair (third‑party module) : Sends requests to the backend with the shortest response time.

upstream nginxDemo {
    server 127.0.0.1:8081; # server A
    server 127.0.0.1:8082; # server B
    fair;
}

The article ends with a note that more advanced configurations will be covered later.

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.

BackendOperationsload balancingNginxreverse proxyTomcat
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.