التوجيه إلى الخدمات المصغّرة المستند إلى المسار
استخدموا مطابقة location في Nginx لتوجيه مسارات URL المختلفة إلى خدمات مصغّرة خلفية مختلفة عبر نقطة دخول واحدة.
التوجيه إلى الخدمات المصغّرة المستند إلى المسار درس مجاني في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
One Door, Many Services
In a microservice system, clients should hit a single host while Nginx fans requests out to the right service based on the URL path.
This keeps clients simple and hides the internal topology.
The location Directive
Routing decisions live in location blocks. Each matches a path pattern and proxies to a specific upstream.
location /users/ {
proxy_pass http://users-service/;
}
location /orders/ {
proxy_pass http://orders-service/;
}Defining Upstreams
Name each service in an upstream block so locations stay readable and you can scale instances later.
upstream users-service {
server 10.0.0.1:8080;
}
upstream orders-service {
server 10.0.0.2:8080;
}Prefix vs Exact Match
A plain location /path/ is a prefix match. Use = for an exact match, which is faster and avoids accidental overlaps.
location = /health {
return 200 "ok";
}The Trailing Slash Rule
A trailing slash on proxy_pass changes path rewriting. With proxy_pass http://svc/; the matched prefix is stripped; without it the full path is forwarded.
location /users/ {
proxy_pass http://users-service/;
}
# /users/42 -> upstream sees /42Regex Locations
Prefix with ~ for case-sensitive regex matching, useful for routing by file type or dynamic segments.
location ~ ^/api/v[0-9]+/payments {
proxy_pass http://payments-service;
}Matching Priority
Nginx evaluates locations in a defined order: exact =, then literal prefixes, then regex in file order. Understanding this prevents surprising routes.
A Default Catch-All
Add a fallback location to serve a friendly response for unknown paths instead of leaking a default error.
location / {
return 404 "Unknown service";
}Shared Proxy Settings
Keep forwarding headers consistent across all services by including a shared snippet in each location.
location /users/ {
include /etc/nginx/proxy_headers.conf;
proxy_pass http://users-service/;
}Adding a New Service
Onboarding a service is just a new upstream plus a new location, no client changes required, which is the whole point of a gateway.
upstream search-service { server 10.0.0.3:8080; }
location /search/ {
proxy_pass http://search-service/;
}Testing the Routes
After reloading, curl each path and confirm it reaches the intended service.
curl http://localhost/users/42
curl http://localhost/orders/7Quick Check
With location /users/ { proxy_pass http://users-service/; }, what path does the upstream receive for a request to /users/42?
Recap
You built path-based routing to multiple services:
locationblocks map paths to upstreams- Trailing slash on
proxy_passcontrols prefix stripping - Match types: exact
=, prefix, and~regex - A catch-all handles unknown paths
One host now cleanly fronts an entire fleet of microservices.
الأسئلة الشائعة
هل درس «التوجيه إلى الخدمات المصغّرة المستند إلى المسار» مجاني؟
نعم — نص درس «التوجيه إلى الخدمات المصغّرة المستند إلى المسار» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)، انتقل إلى CoddyKit PRO. تتضمن دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 4 دروس في المجموع.
ماذا ستتعلم في «التوجيه إلى الخدمات المصغّرة المستند إلى المسار»؟
استخدموا مطابقة location في Nginx لتوجيه مسارات URL المختلفة إلى خدمات مصغّرة خلفية مختلفة عبر نقطة دخول واحدة. تتمرن على 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 منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التوجيه إلى الخدمات المصغّرة المستند إلى المسار»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) هذا؟
نعم. كل درس في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إصدارات API باستخدام Nginx
- مشاركة الموارد بين المصادر (CORS)
- تحديد معدل الطلبات وخنقها باستخدام Nginx
- التوجيه إلى الخدمات المصغّرة المستند إلى المسار