Step-by-Step Guide to Installing Nginx and Tomcat on Two Virtual Machines and Configuring Load Balancing
This tutorial explains how to set up two virtual machines, install JDK, Nginx and Tomcat on each, configure Nginx as a reverse‑proxy load balancer for the Tomcat instances, and verify the high‑availability setup with sample HTML pages.
Prepare two virtual machines with IPs 192.168.56.9 and 192.168.56.10; all subsequent software installations and configurations must be performed on both machines.
0x01: Nginx Installation – Follow the referenced Nginx series article for source compilation and installation.
0x02: Tomcat Installation
Tomcat requires a JDK runtime, so install JDK first. Both JDK and Tomcat are distributed as compressed archives and can be installed by extracting them.
Extract JDK: tar -zxvf jdk-8u161-linux-x64.tar.gz Configure JAVA_HOME and update environment variables (add to /etc/profile or .bash_profile):
JAVA_HOME=/usr/jdk1.8.0_161
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATHDownload and extract Tomcat:
wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-8/v8.5.64/bin/apache-tomcat-8.5.64.tar.gz tar -zxvf apache-tomcat-8.5.64.tar.gzStart Tomcat: ./bin/startup.sh Verify that Tomcat starts successfully on each VM.
0x03: Configure Load Balancing
Use Nginx as a reverse‑proxy load balancer. The essential configuration is shown below:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
upstream tomcat_server {
server 192.168.56.10:8080 weight=1;
server 192.168.56.9:8080 weight=1;
}
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_server;
}
error_page 400 =400.html;
error_page 404 =404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}Key points: define an upstream block with the two Tomcat servers, and configure a server block that proxies all requests to this upstream.
0x04: Verification
Create simple HTML pages in each Tomcat's webapps/ROOT directory to identify which instance serves the request:
<html>
<body>
<h1>Tomcat : 192.168.56.9</h1>
</body>
</html> <html>
<body>
<h1>Tomcat : 192.168.56.10</h1>
</body>
</html>Start both Tomcat instances and access the Nginx address. Requests will be distributed between the two Tomcat servers, demonstrating load balancing. Shutting down one Tomcat still allows access, confirming high availability of the application layer. The next step (not covered here) is to achieve Nginx high availability using Keepalived.
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.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.
