Load balancing distributes incoming network traffic across multiple servers to ensure no single server becomes overwhelmed. This technique enhances application availability, scalability, and reliability. Nginx, a high-performance web server, can also function as an effective software load balancer for applications like those built with Spring Boot.
Load Balancing Methods in Nginx
Nginx supports several algorithms for distributing client requests among backend servers.
Round Robin This default method forwards requests sequentially to each server in the list.
upstream backend_servers {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
Least Connections Directs traffic to the server currently handling the fewest active connections.
upstream backend_servers {
least_conn;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
IP Hash Uses the client's IP address to determine which server receives the request, ensuring a user consistently reaches the same backend server.
upstream backend_servers {
ip_hash;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
Generic Hash Distributes requests based on a user-defined key, which could be a variable, text string, or combination.
upstream backend_servers {
hash $request_uri consistent;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
Configuring Nginx for a Spring Boot Application
To balance load between multiple instances of a Spring Boot application, you must define an upstream block and configure a server block to proxy requests.
First, package your Spring Boot application into an executable JAR. Create multiple copies or start the application multiple times with different server ports, for instance, 8080 and 8081.
Edit the Nginx configuration file (nginx.conf). Define an upstream block naming your backend servers.
http {
upstream app_cluster {
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://app_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 3s;
proxy_read_timeout 10s;
}
}
}
upstream app_cluster: Defines a group namedapp_clustercontaining the backend servers.proxy_pass http://app_cluster;: Forwrads requests to the defined server group.- The
proxy_set_headerdirectives ensure original request information is passed to the backend servers. - Timeout directives like
proxy_connect_timeouthelp manage connection behavior.
Validation and Testing
- Start your Spring Boot application instances on their respective ports (e.g., 8080 and 8081).
- Start or reload Nginx to apply the new configuration.
nginx -s reload - Send multiple HTTP requests to Nginx's address (e.g.,
http://localhost). The requests should be distributed between the two backend servers. Check the application logs on each instance to confirm the distribution.
Important Configuration Notes
When applications run on non-standard ports, especially those with login or redirect logic, you may need to explicitly set the Host header to prevent issues like ERR_NAME_NOT_RESOLVED. The configuration proxy_set_header Host $host:$server_port; can resolve this by preserving the original port information.
Using Nginx as a load balancer for Spring Boot applications is a straightforward process that significantly improves the robustness of your service architecture.