Master Nginx Load Balancing: Strategies, Configurations, and SpringBoot Integration
This guide explains Nginx load balancing concepts, compares hardware and software approaches, details various balancing algorithms with configuration examples, and shows how to integrate Nginx with SpringBoot for practical load‑balancing tests and deployment tips.
Introduction
This article introduces how Nginx implements load balancing.
Load Balancing Overview
Introduction
Before discussing Nginx load balancing, the article briefly explains the classification of load balancing: hardware versus software. Hardware uses dedicated appliances like F5, while software such as Nginx provides a message‑queue‑like distribution mechanism.
Load balancing distributes incoming requests across multiple servers. For example, with three servers A, B, C and a round‑robin policy, nine requests are evenly sent three to each server, reducing the load on any single machine.
Load Balancing Strategies
Nginx open‑source supports four methods, Nginx Plus adds two more.
Round Robin – default, cycles through servers.
Least Connections – sends request to server with fewest active connections, can consider weight.
IP Hash – selects server based on client IP, ensuring the same client reaches the same server unless it is unavailable.
Generic Hash – uses a user‑defined key (string, variable, or combination) to choose the server.
Least Time (NGINX Plus) – selects server with lowest average latency and fewest active connections, based on header, last_byte, or last_byte inflight.
Random – picks a server at random; with two parameters it first selects two servers based on weight and then applies a method such as least_conn or least_time.
Configuration examples:
upstream xuwujing {<br/> server www.panchengming.com;<br/> server www.panchengming2.com;<br/>} upstream xuwujing {<br/> least_conn;<br/> server www.panchengming.com;<br/> server www.panchengming2.com;<br/>} upstream xuwujing {<br/> ip_hash;<br/> server www.panchengming.com;<br/> server www.panchengming2.com;<br/>} upstream xuwujing {<br/> hash $request_uri consistent;<br/> server www.panchengming.com;<br/> server www.panchengming2.com;<br/>} upstream xuwujing {<br/> least_time header;<br/> server www.panchengming.com;<br/> server www.panchengming2.com;<br/>} upstream xuwujing {<br/> random two least_time=last_byte;<br/> server www.panchengming.com;<br/> server www.panchengming2.com;<br/>}Nginx+SpringBoot Load Balancing
Environment Preparation
JDK 1.8 or higher.
Nginx installed.
Package the SpringBoot project with mvn clean package, place the generated jar and application.properties in a folder, duplicate the folder, and modify the port in application.properties (e.g., 8086).
Nginx Configuration
Edit nginx.conf to add an upstream block:
upstream pancm{<br/> server 127.0.0.1:8085;<br/> server 127.0.0.1:8086;<br/>}Then configure the server block:
server {<br/> listen 80;<br/> server_name 127.0.0.1;<br/> location / {<br/> root html;<br/> proxy_pass http://pancm;<br/> proxy_connect_timeout 3s;<br/> proxy_read_timeout 5s;<br/> proxy_send_timeout 3s;<br/> index index.html index.htm;<br/> }<br/> error_page 500 502 503 504 /50x.html;<br/> location = /50x.html {<br/> root html;<br/> }<br/>}A complete nginx.conf example is provided in the original article.
Load Balancing Test
Start Nginx (Linux:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confor reload with -s reload; Windows: run nginx.exe or start nginx).
Start the two SpringBoot instances (e.g., java -jar springboot-jsp-thymeleaf.jar) and access the service IP in a browser. The example screenshots show requests being evenly distributed across the two services.
Note: When the application uses a non‑standard port or login pages, the default proxy may cause net::ERR_NAME_NOT_RESOLVED. Add proxy_set_header Host $host:port inside the location block, matching the listen port.
Author: 虚无境 Source: www.cnblogs.com/xuwujing/p/11953697.html Copyright statement: content is shared for learning only; original author retains rights.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
