0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · บทเรียน

การกำหนดเส้นทางไปยังไมโครเซอร์วิสตามพาธ

ใช้การจับคู่ location ของ Nginx เพื่อกำหนดเส้นทาง URL ต่าง ๆ ไปยังไมโครเซอร์วิสแบ็กเอนด์ที่แตกต่างกันผ่านจุดทางเข้าเดียว

การกำหนดเส้นทางไปยังไมโครเซอร์วิสตามพาธ เป็นบทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 /42

Regex 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/7

Quick 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:

  • location blocks map paths to upstreams
  • Trailing slash on proxy_pass controls prefix stripping
  • Match types: exact =, prefix, and ~ regex
  • A catch-all handles unknown paths

One host now cleanly fronts an entire fleet of microservices.

คำถามที่พบบ่อย

บทเรียน “การกำหนดเส้นทางไปยังไมโครเซอร์วิสตามพาธ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การกำหนดเส้นทางไปยังไมโครเซอร์วิสตามพาธ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การกำหนดเวอร์ชัน API ด้วย Nginx
  2. การแชร์ทรัพยากรข้ามต้นทาง (CORS)
  3. การจำกัดอัตราและการควบคุมปริมาณด้วย Nginx
  4. การกำหนดเส้นทางไปยังไมโครเซอร์วิสตามพาธ
← กลับไปที่ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)