How to Build Nginx Reverse Proxy Load Balancing with Tomcat and Redis Session Sharing

This step‑by‑step guide shows how to set up two Tomcat instances, configure Nginx as a reverse‑proxy load balancer, and use Redis to share session data, solving session‑loss issues in a simple load‑balanced environment.

ITPUB
ITPUB
ITPUB
How to Build Nginx Reverse Proxy Load Balancing with Tomcat and Redis Session Sharing

Load Balancing Overview

Load balancing distributes incoming client requests across multiple servers with equal status, improving concurrency handling, response speed, and overall availability.

Environment Preparation

Install two Tomcat instances on the same machine and modify their server.xml ports to avoid conflicts.

Create a simple web application and deploy the WAR to each Tomcat's webapps directory.

Start both Tomcat servers and verify that each serves distinct content, confirming they operate as independent back‑ends.

Install and Start Nginx

Download a pre‑built Nginx package (e.g., from http://www.pcre.org/), unzip it, and run start nginx from the command line. The Nginx process appears in the task manager, and http://localhost shows the default welcome page.

Configure Nginx as a Reverse Proxy

Edit conf/nginx.conf and replace the default server block with the following configuration (adjust the Tomcat ports as needed):

http {
    upstream tomcat_cluster {
        server 127.0.0.1:8080 weight=1;
        server 127.0.0.1:8081 weight=1;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://tomcat_cluster;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

Reload Nginx with nginx -s reload. Accessing http://localhost now alternates between the two Tomcat pages, demonstrating basic round‑robin load balancing.

Session Synchronization Issue

Because each Tomcat instance stores sessions locally, a user’s session is lost when requests are routed to a different Tomcat. To share sessions, integrate Redis as a centralized session store.

Deploy Redis Session Manager JARs

Copy the following JAR files into ${TOMCAT_HOME}/lib:

tomcat-redis-session-manager-VERSION.jar
jedis-2.5.2.jar
commons-pool2-2.2.jar

These JARs can be downloaded from: http://bbs.chinaunix.net/thread-4262624-1-1.html

Configure Tomcat to Use Redis

Edit ${TOMCAT_HOME}/conf/context.xml and add the following elements inside the <Context> tag:

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="localhost"
         port="6379"
         database="0"
         maxInactiveInterval="60" />

Replace host and port with the actual Redis server address. Restart both Tomcat instances.

Result

With Nginx performing reverse‑proxy load balancing, two Tomcat instances serving the same application, and Redis providing a shared session store, the system achieves high availability, balanced traffic distribution, and consistent user sessions across all back‑ends.

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.

Backendload balancingredisNGINXTomcatsession sharing
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.