Backend Development 8 min read

Implementing Load Balancing with Nginx and SpringBoot

This article explains how Nginx can be used to achieve various load‑balancing strategies—including round‑robin, least‑connections, IP hash, generic hash, least‑time, and random—provides detailed configuration examples, shows how to integrate Nginx with SpringBoot services, and outlines testing and deployment steps.

Architecture Digest
Architecture Digest
Architecture Digest
Implementing Load Balancing with Nginx and SpringBoot

The article begins with an overview of load balancing, distinguishing hardware and software approaches, and defines load balancing as distributing incoming requests across multiple servers to reduce individual server load.

It then introduces Nginx as a software load balancer and lists the six load‑balancing methods supported by the open‑source version (Round Robin, Least Connections, IP Hash, Generic Hash, Random) plus two additional methods available in Nginx Plus (Least Time based on header or last byte).

For each method, a concise Nginx upstream configuration example is provided:

upstream xuwujing { server www.panchengming.com; server www.panchengming2.com; }

upstream xuwujing { least_conn; server www.panchengming.com; server www.panchengming2.com; }

upstream xuwujing { ip_hash; server www.panchengming.com; server www.panchengming2.com; }

upstream xuwujing { hash $request_uri consistent; server www.panchengming.com; server www.panchengming2.com; }

upstream xuwujing { least_time header; server www.panchengming.com; server www.panchengming2.com; }

upstream xuwujing { random two least_time=last_byte; server www.panchengming.com; server www.panchengming2.com; }

Next, the article shows how to combine Nginx with a SpringBoot application. After building the SpringBoot JAR, the user places the JAR and its application.properties in a folder, adjusts the port, and defines an Nginx upstream named pancm that points to the two SpringBoot instances (e.g., 127.0.0.1:8085 and 127.0.0.1:8086 ).

The full nginx.conf is presented, including the events block, global settings, the upstream pancm definition, and a server block that listens on port 80, sets proxy_pass http://pancm , and configures timeout and error handling directives.

Deployment steps are described: start or reload Nginx, launch the SpringBoot JARs, and access the service via a browser. Sample screenshots illustrate the load‑balancing effect, showing requests being evenly distributed between the two backend services.

Finally, the article notes a common pitfall when the application uses a non‑standard port: the default Nginx host header points to port 80, causing net::ERR_NAME_NOT_RESOLVED errors. The fix is to add proxy_set_header Host $host:port inside the location block, matching the backend port.

proxybackend developmentLoad BalancingconfigurationnginxSpringBoot
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.