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

Nginx 位置块与请求匹配

掌握 Nginx 位置块及其匹配规则,了解哪些配置会处理每个传入请求。

第 4 / 4 课13 个步骤

Nginx 位置块与请求匹配 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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
免费开始

用 AI 导师学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「Nginx 位置块与请求匹配」课时是免费的吗?

是的 — 「Nginx 位置块与请求匹配」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

「Nginx 位置块与请求匹配」这节课中我会学到什么?

掌握 Nginx 位置块及其匹配规则,了解哪些配置会处理每个传入请求。 你通过在浏览器中直接运行的动手代码来练习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Nginx 位置块与请求匹配」课时需要多长时间?

大多数 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 位置块与请求匹配
← 返回 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)