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

Blok Location Nginx & Pencocokan Permintaan

Kuasai blok location Nginx dan aturan pencocokan yang menentukan konfigurasi untuk menangani setiap permintaan masuk.

Blok Location Nginx & Pencocokan Permintaan 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.

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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Blok Location Nginx & Pencocokan Permintaan” gratis?

Ya — teks lengkap “Blok Location Nginx & Pencocokan Permintaan” 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 “Blok Location Nginx & Pencocokan Permintaan”?

Kuasai blok location Nginx dan aturan pencocokan yang menentukan konfigurasi untuk menangani setiap permintaan 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 “Blok Location Nginx & Pencocokan Permintaan” 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. Instalasi dan Penyiapan Nginx
  2. Konfigurasi Dasar Nginx
  3. Menyajikan Konten Statis dengan Nginx
  4. Blok Location Nginx & Pencocokan Permintaan
← Kembali ke API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)