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

Nginxのlocationブロックとリクエストマッチング

各受信リクエストをどの設定で処理するかを決める、Nginxのlocationブロックとマッチングルールを身につけます。

「Nginxのlocationブロックとリクエストマッチング」は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レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Location Blocks Matter

A real site serves many paths differently: static files, an API, an admin area. Location blocks let Nginx apply different rules per URL path.

Anatomy of a Location

A location block matches a request URI and contains directives for it.

location /images/ {
  root /var/www;
}

Prefix Matching

By default a location is a prefix match: location /blog matches /blog, /blog/post, and anything starting with it.

location /blog {
  # matches /blog, /blog/, /blog/anything
}

Exact Match with =

The = modifier requires an exact URI. It is fast and great for hot paths like the health endpoint or homepage.

location = /health {
  return 200 'ok';
}

Regex Matching

Use ~ for case-sensitive and ~* for case-insensitive regular expressions, handy for file extensions.

location ~* \.(jpg|png|gif)$ {
  expires 30d;
}

The Matching Priority

Nginx does not just pick the first match. Order of precedence:

  • = exact match wins immediately
  • ^~ prefix stops regex search
  • regex (~/~*) in file order
  • longest plain prefix as fallback

The ^~ Modifier

^~ means: if this prefix is the longest match, use it and skip regex checking. Useful to protect a static directory from regex rules.

location ^~ /static/ {
  root /var/www;
}

Longest Prefix Wins

Among plain prefixes, the longest match is chosen, not the first listed. /api/v2 beats /api for a request to /api/v2/users.

Combining with proxy_pass

Location blocks commonly route to backends. Different paths can proxy to different upstreams.

location /api/ {
  proxy_pass http://127.0.0.1:8080;
}

Nested and Named Locations

Locations can nest inside others, and named locations (@name) are jumped to internally, often for fallbacks like try_files.

location / {
  try_files $uri @app;
}
location @app {
  proxy_pass http://backend;
}

Debugging Matches

When a request hits the wrong block, check: is there an exact or ^~ match stealing it? Is a regex matching unexpectedly? Reload and test with curl -v.

nginx -t && nginx -s reload

Quick Check

Test your location matching knowledge.

Recap

You learned Nginx location matching:

  • Prefix match is the default; = is exact
  • Regex uses ~ and ~*
  • Priority: exact, then ^~, then regex, then longest prefix
  • Longest prefix wins among plain matches
  • Named locations enable fallbacks; test with nginx -t and reload

よくある質問

「Nginxのlocationブロックとリクエストマッチング」レッスンは無料ですか?

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

「Nginxのlocationブロックとリクエストマッチング」で何を学びますか?

各受信リクエストをどの設定で処理するかを決める、Nginxのlocationブロックとマッチングルールを身につけます。 ブラウザで直接実行するハンズオンコードで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です。

「Nginxのlocationブロックとリクエストマッチング」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. Nginxのインストールとセットアップ
  2. Nginxの基本設定
  3. Nginxで静的コンテンツを配信する
  4. Nginxのlocationブロックとリクエストマッチング
← API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)に戻る