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
Over 2 million developers have joined DZone.
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

"Platform Engineering & DevOps" Trend Report is now LIVE! Learn how internal platforms help developers ship faster with less friction

Join this live webinar to learn safer rollout techniques for schema changes, index testing, and database migrations.

Live Webinar: Exclusive practitioner summit on AI-powered CDN operations and real-world automation strategies

Related

  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Zero Trust, Build High Scale TLS Termination Layer
  • Why Your "Stateless" Services Are Lying to You

Trending

  • The Repo Tracker: Automating My Daily GitHub Catch-Up
  • Reducing RAG Hallucinations With Relationship-Aware Retrieval
  • How to Build an Agentic AI SRE Co-Pilot for Incident Response
  • 7 Technology Waves I’ve Seen in 30 Years of Software — Will AI Be the Next Real Transformation?
  1. DZone
  2. Coding
  3. Languages
  4. Load Balancing Apache Tomcat with Nginx

Load Balancing Apache Tomcat with Nginx

By 
Ross Mason user avatar
Ross Mason
·
Sep. 03, 13 · Interview
Likes (1)
Comment
Save
Tweet
Share
37.0K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Zero Trust, Build High Scale TLS Termination Layer
  • Why Your "Stateless" Services Are Lying to You

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

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:

  • RSS
  • X
  • Facebook