How to Configure Nginx Load Balancing for Multiple Web Servers
This guide explains how to set up Nginx as a reverse proxy, create an upstream server group, and configure load‑balancing directives so that two web servers can share traffic and handle increased load efficiently.
Assume you have two servers: an Nginx server acting as a reverse proxy and a web server (e.g., Tomcat). Nginx forwards all requests to the web server.
When traffic grows, the single web server becomes overloaded. Adding a second web server and using Nginx load balancing allows both servers to work together.
Load‑Balancing Configuration Steps
1. Add an upstream server group
http {
...
upstream backend {
server web1.example.com;
server web2.example.com;
}
}The upstream block defines a server group named backend; each server directive specifies a web server address (domain or IP). The upstream block must be placed inside the http context.
2. Forward requests to the server group
Modify the original server block to proxy traffic to the upstream group:
server {
...
location / {
proxy_pass http://backend;
}
}More configuration options
Nginx offers richer load‑balancing settings, for example:
location / {
proxy_pass http://backend;
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
include /opt/nginx/conf/proxy.conf;
}The proxy_next_upstream directive defines failover behavior; if a backend returns a 500‑type error, Nginx automatically retries the request on another server in the upstream group. Multiple error types can be listed.
The include directive imports a reusable proxy.conf file for common settings, such as:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_buffers 4 32k;
proxy_buffer_size 4k;
proxy_busy_buffers_size 64k;These directives set the host header, preserve the client’s real IP, define buffer sizes, and configure timeouts to ensure reliable proxying under high load.
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.
