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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
