DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones Build AI Agents That Are Ready for Production
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
Build AI Agents That Are Ready for Production

Connect coding agents to pipeline feedback. Join DZone and Harness for a live webinar December 8.

Related

  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS
  • How to Build a Scalable SaaS Product Architecture
  • Why Your MCP Server Loses Session State Behind a Load Balancer
  • Understand the Sidecar Pattern by Deploying n8n to AWS Fargate

Trending

  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud
  • A Software Factory on Kubernetes: Gemini Builds, jev Decides (Genkit + senro in Go)
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Load Balancing Apache Tomcat with Nginx

Load Balancing Apache Tomcat with Nginx

By 
Ross Mason
·
Sep. 03, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
37.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This post comes from Karan Malhi at the Mulesolft blog.

Nginx (pronounced "engine X") is an HTTP and reverse proxy server. It is well known for its high performance and stability. It is pretty feature-rich and very simple to configure. Nginx hosts nearly 12.18 percent (22.2M) of active sites across all domains. Nginx uses event-driven architecture to handle requests. When compared to a thread-per-request model, event-driven is highly scalable with a low and predicatble memory footprint.

Nginx is very easy to set up as a load balancer for an Apache Tomcat farm. In this blog post, I will show you how to set it up as a round-robin load balancer for two ApacheTomcat servers.

You first need to install Nginx, you can find the installation instructions here. If you are on a Mac, then you could use Homebrew

brew install nginx

After installing, you can run it using the nginx command

 sudo nginx

You can now test the installation by opening the following URL in a browser:

 http://localhost:8080

Lets change the default port 8080 to port 80. You can do that in the nginx.conf file which by default is located at /usr/local/etc/nginx/nginx.conf. First, stop nginx:

 sudo nginx -s stop 

Now, open the nginx.conf file and locate the listen attribute of the server. Change the value from 8080 to 80. Here is where I made the change in my configuration:

server { 
listen 80; 
server_name localhost;

Save the file and start nginx. You should now be able to test the configuration change by visiting http://localhost

Install two instances of Apache Tomcat. Open their server.xml files and change the HTTP port numbers to 8080 and 8081 respectively. You can find more information on Apache Tomcat configuration here. Once you have made the changes and saved the configuration files, you should now start each instance of Apache Tomcat. Here are a few ways you can start Apache Tomcat. Typically you should be able to locate the bin directory inside your Tomcat installation and invoke the startup.sh file as shown:

 bin/startup.sh

Now that the Tomcat instances are started, let's set up the round robin load balancer. You would need to use the Nginx upstream module. The upstream module allows you to group servers that can be referenced by the proxy_pass directive. In your nginx.conf, locate the http block and add the upstream block to create a group of servers:

http { 
upstream tomcat_servers{ 
ip_hash; server 127.0.0.1:8080; 
server 127.0.0.1:8081; 
} 
ip_hash above configures the load balancing method where requests are routed to servers based on IP Addresses of the client, so a request from a client with a particular IP will always go to the same back end Tomcat Server.

Finally, you need to locate the location block within the server block and map the root(/) location to the tomcat_servers group created using the upstream module above.

location / { 
proxy_pass http://tomcat_servers; 
}

That's it!. Restart Nginx and you should now be able to send a request to http://localhost and the request will be served by one of the Tomcat servers.



Apache Tomcat Load balancing (computing)

Published at DZone with permission of Ross Mason. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS
  • How to Build a Scalable SaaS Product Architecture
  • Why Your MCP Server Loses Session State Behind a Load Balancer
  • Understand the Sidecar Pattern by Deploying n8n to AWS Fargate

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • ⭐ Must Read: articles/load balancing apache tomcat - Latest Analysis
  • ⭐ Must Read: articles/load balancing apache tomcat - Latest Analysis
  • ⭐ Must Read: articles/load balancing apache tomcat - Latest Analysis

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • ⭐ Must Read: articles/load balancing apache tomcat - Latest Analysis
  • ⭐ Must Read: articles/load balancing apache tomcat - Latest Analysis
  • ⭐ Must Read: articles/load balancing apache tomcat - Latest Analysis