Implementing Load Balancing with Nginx and SpringBoot
This article explains how to achieve load balancing using Nginx, covering the concepts of hardware and software load balancers, various Nginx balancing algorithms with configuration examples, and a step‑by‑step guide to integrate Nginx with a SpringBoot application, test it, and handle common pitfalls.
The article introduces Nginx load balancing, first distinguishing hardware load balancers (e.g., F5) from software solutions like Nginx, and explains that load balancing distributes incoming requests across multiple servers to reduce individual server load.
It then describes several Nginx load‑balancing methods: Round Robin (default), Least Connections, IP Hash, Generic Hash, Least Time (NGINX Plus only), and Random, each accompanied by a concrete upstream configuration snippet.
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 service. It lists the required environment (JDK 1.8+, Nginx), describes packaging the SpringBoot project into a JAR, and modifying application.properties to set a custom port.
The Nginx configuration adds an upstream block named pancm that points to two local SpringBoot instances (ports 8085 and 8086), and a server block that proxies all traffic to this upstream. Key directives such as proxy_pass , timeout settings, and error handling are explained.
upstream pancm{
server 127.0.0.1:8085;
server 127.0.0.1:8086;
} server {
listen 80;
server_name 127.0.0.1;
location / {
root html;
proxy_pass http://pancm;
proxy_connect_timeout 3s;
proxy_read_timeout 5s;
proxy_send_timeout 3s;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; }
}A complete nginx.conf file is provided, showing the event block, HTTP settings, the upstream definition, and the server block.
For testing, the article instructs to start Nginx (using the appropriate command for Linux or Windows), launch the two SpringBoot JARs, and then access the service via a browser. It includes screenshots demonstrating that repeated requests are evenly distributed between the two backend instances.
Finally, it notes a common issue when the application uses a non‑standard port or login pages: the default Nginx port (80) can cause redirects to fail. The solution is to add proxy_set_header Host $host:port inside the location block, ensuring the host header matches the actual service port.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.