Backend Development 10 min read

Nginx Introduction, Installation, Load‑Balancing Configuration and Tomcat Setup on CentOS 7

This guide explains what Nginx is and how reverse proxy works, details its load‑balancing algorithms, provides step‑by‑step commands to download, compile and install Nginx and Tomcat on CentOS 7, shows how to configure Nginx upstreams for Tomcat servers, and lists common pitfalls such as firewall blocks.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Nginx Introduction, Installation, Load‑Balancing Configuration and Tomcat Setup on CentOS 7

Nginx Overview – Nginx is a free, open‑source, high‑performance HTTP server and reverse proxy used by major Chinese internet companies. A reverse proxy receives external requests, forwards them to internal servers, and returns the responses to clients.

Load‑Balancing Algorithms

Weight (round‑robin) – distributes requests sequentially; servers can be weighted to receive more traffic.

ip_hash – hashes the client IP so the same client always reaches the same backend, helping with session sharing.

fair – dynamically balances based on response time (requires the upstream_fair module).

url_hash – hashes the request URL to a fixed backend (requires the hash module).

Installing Nginx on CentOS 7

Download the tarball (e.g., wget -c https://nginx.org/download/nginx-1.6.3.tar.gz ), install prerequisites, extract, configure and compile:

yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

tar -zxvf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure
make install
whereis nginx
cd /usr/local/nginx/sbin
./nginx   # start
./nginx -s stop   # stop
./nginx -s quit   # graceful stop
./nginx -s reload # reload config

Modify nginx.conf (e.g., change listen 80 to listen 83 ) and reload.

Installing Tomcat

Download and extract the tarball, rename directories, and adjust ports in server.xml :

wget http://apache.mirrors.tds.net/tomcat/tomcat-8/v8.5.40/bin/apache-tomcat-8.5.40.tar.gz
tar -zxvf apache-tomcat-8.5.40.tar.gz
mv apache-tomcat-8.5.40 tomcat-1
mv apache-tomcat-8.5.40 tomcat-2
# edit server.xml to set ports 8080 and 8081, adjust shutdown, AJP, etc.
cd tomcat-1/bin
sh startup.sh
cd tomcat-2/bin
sh startup.sh
ps -ef|grep tomcat

Create simple index.html files in each webapps/ROOT to verify access via http:// ip :8080 and http:// ip :8081 .

Nginx Load‑Balancing Configuration

Edit /usr/local/nginx/conf/nginx.conf and add an upstream block pointing to the two Tomcat instances:

upstream test.com {
    server 127.0.0.1:8080 weight=1;
    server 127.0.0.1:8081 weight=2;
}
server {
    listen 83;
    server_name localhost;
    location / {
        proxy_pass http://test.com;
        proxy_redirect default;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html { root html; }
}

Key parameters explained:

worker_processes – number of worker processes (usually equals CPU cores).

worker_connections – max connections per worker.

sendfile – enables efficient file transmission.

keepalive_timeout – timeout for persistent connections.

upstream – defines the backend server pool.

After editing, reload Nginx with ./nginx -s reload and test by repeatedly refreshing http:// ip :83 . The load‑balancing effect can be observed.

Common Failure Reason

Many users cannot reach the Nginx page because the firewall blocks incoming traffic; ensure the appropriate ports are open.

backendload balancingNginxtomcatServer ConfigurationCentOS
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login 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.