0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · レッスン

パスベースのマイクロサービスルーティング

Nginxのlocationマッチングを使い、1つのエントリーポイントの背後にある異なるバックエンドマイクロサービスへ、URLパスごとにルーティングします。

「パスベースのマイクロサービスルーティング」はCoddyKit上の無料API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.

よくある質問

「パスベースのマイクロサービスルーティング」レッスンは無料ですか?

はい。「パスベースのマイクロサービスルーティング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)コースには全4レッスンが含まれています。

「パスベースのマイクロサービスルーティング」で何を学びますか?

Nginxのlocationマッチングを使い、1つのエントリーポイントの背後にある異なるバックエンドマイクロサービスへ、URLパスごとにルーティングします。 ブラウザで直接実行するハンズオンコードでAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)を始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「パスベースのマイクロサービスルーティング」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンでコードを書いて実行できますか?

はい。すべてのAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. NginxによるAPIバージョニング
  2. Cross-Origin Resource Sharing(CORS)
  3. Nginxによるレート制限とスロットリング
  4. パスベースのマイクロサービスルーティング
← API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)に戻る