API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Pelajaran

Perutean Berbasis Jalur ke Layanan Mikro

Gunakan pencocokan location Nginx untuk merutekan berbagai jalur URL ke layanan mikro backend yang berbeda di balik satu titik masuk.

Pelajaran 4 dari 413 langkah

Perutean Berbasis Jalur ke Layanan Mikro adalah pelajaran API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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.

Gratis untuk memulai

Belajar API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) dengan tutor AI — gratis

Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.

Kursus
12
Pelajaran
48

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Perutean Berbasis Jalur ke Layanan Mikro” gratis?

Ya — teks lengkap “Perutean Berbasis Jalur ke Layanan Mikro” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), upgrade ke CoddyKit PRO. Kursus API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Perutean Berbasis Jalur ke Layanan Mikro”?

Gunakan pencocokan location Nginx untuk merutekan berbagai jalur URL ke layanan mikro backend yang berbeda di balik satu titik masuk. Kamu berlatih API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Tidak diperlukan pengalaman sebelumnya. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Perutean Berbasis Jalur ke Layanan Mikro” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ini?

Ya. Setiap pelajaran API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Pembuatan Versi API dengan Nginx
  2. Berbagi Sumber Daya Lintas Asal (CORS)
  3. Pembatasan Laju dan Pengendalian Arus dengan Nginx
  4. Perutean Berbasis Jalur ke Layanan Mikro
← Kembali ke API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)