الخوادم الوسيطة وموازنة التحميل
اضبطوا كتل upstream في Nginx لإدارة عدة خوادم خلفية وتوزيع الطلبات الواردة.
الخوادم الوسيطة وموازنة التحميل درس مجاني في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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!
الأسئلة الشائعة
هل درس «الخوادم الوسيطة وموازنة التحميل» مجاني؟
نعم — نص درس «الخوادم الوسيطة وموازنة التحميل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)، انتقل إلى CoddyKit PRO. تتضمن دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 4 دروس في المجموع.
ماذا ستتعلم في «الخوادم الوسيطة وموازنة التحميل»؟
اضبطوا كتل upstream في Nginx لإدارة عدة خوادم خلفية وتوزيع الطلبات الواردة. تتمرن على API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)؟
لا تُشترط خبرة سابقة. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الخوادم الوسيطة وموازنة التحميل»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) هذا؟
نعم. كل درس في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد وكيل عكسي بسيط
- الخوادم الوسيطة وموازنة التحميل
- التخزين المؤقت للوكيل والتخزين المخبئي
- تمرير الترويسات وعنوان IP الخاص بالعميل