上游服务器与负载均衡
配置 Nginx 上游块,以管理多个后端服务器并分发传入请求。
上游服务器与负载均衡 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Scaling Your Applications
As your web application grows, a single server might not be enough to handle all incoming requests. This can lead to slow response times or even crashes.
To prevent this, we use multiple backend servers to share the load. This improves both performance and reliability.
Introducing Upstream Servers
Nginx can act as a reverse proxy, distributing requests to a group of backend servers. These backend servers are often called upstream servers.
- Scalability: Handle more traffic by adding more servers.
- High Availability: If one server fails, others can still respond.
- Load Balancing: Distribute requests efficiently among servers.
The `upstream` Block
In Nginx, you define a group of backend servers using the upstream block. This block gives a name to your group of servers, which you can then reference in your location blocks.
Think of it as a named pool of resources that Nginx can forward requests to.
Basic Upstream Configuration
Here's how to define a simple upstream block named my_backends with two servers. Each server directive specifies the address (IP or hostname) and port of a backend server.
upstream my_backends {
server 192.168.1.100:8080;
server 192.168.1.101:8080;
}Connecting Upstream to `location`
Once an upstream block is defined, you can use it within a location block in your Nginx server configuration. The proxy_pass directive tells Nginx to forward requests to the specified upstream group.
Notice we use http://my_backends to reference our defined group.
http {
upstream my_backends {
server 192.168.1.100:8080;
server 192.168.1.101:8080;
}
server {
listen 80;
location /app {
proxy_pass http://my_backends;
}
}
}Default Load Balancing: Round Robin
By default, Nginx uses the Round Robin load balancing method. This means requests are distributed to upstream servers in a rotating, sequential manner.
- Request 1 goes to Server A.
- Request 2 goes to Server B.
- Request 3 goes to Server A.
- ...and so on.
It's simple and effective for evenly distributed loads.
Customizing Load with `weight`
What if some servers are more powerful or should handle more traffic? You can use the weight parameter in the server directive to assign different priorities.
A server with a higher weight will receive a proportionally larger share of requests.
`weight` Example in Nginx
In this example, server 1 has a weight of 3, and server 2 has a weight of 1. This means server 1 will receive 3 times as many requests as server 2.
Out of every 4 requests, 3 will go to server 1 and 1 to server 2.
upstream my_backends {
server 192.168.1.100:8080 weight=3;
server 192.168.1.101:8080 weight=1;
}Other Server Directives
Beyond weight, other directives can fine-tune server behavior:
max_fails=N: Number of failed attempts before Nginx considers the server unavailable.fail_timeout=Xs: Time for which the server is considered unavailable aftermax_failsattempts.backup: Marks a server as a backup, used only when all primary servers are down.down: Marks a server as permanently unavailable.
These help Nginx manage server health automatically.
Quick Check: Upstream & Load Balancing
Consider the following Nginx configuration snippet:
upstream api_servers {
server backend1.example.com:8080 weight=2;
server backend2.example.com:8080;
}
server {
listen 80;
location /api {
proxy_pass http://api_servers;
}
}If 9 requests arrive at the /api endpoint, how many requests will be sent to backend1.example.com:8080?
Recap & Next Steps
You've learned how Nginx uses upstream blocks to group multiple backend servers, enabling load balancing and improving application resilience.
upstreamblock: Defines a named group of servers.proxy_pass: Connects alocationto anupstreamgroup.- Round Robin: Nginx's default load balancing method.
weightdirective: Customizes request distribution among servers.
Next, we'll explore how Nginx can improve performance with proxy buffering and caching!
用 AI 导师学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「上游服务器与负载均衡」课时是免费的吗?
是的 — 「上游服务器与负载均衡」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。
「上游服务器与负载均衡」这节课中我会学到什么?
配置 Nginx 上游块,以管理多个后端服务器并分发传入请求。 你通过在浏览器中直接运行的动手代码来练习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「上游服务器与负载均衡」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课中编写并运行代码吗?
能。每节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 配置简单反向代理
- 上游服务器与负载均衡
- 代理缓冲与缓存
- 转发标头与客户端 IP