← [ BACK_TO_NOTES ]

Nginx MOC

Nginx 主题地图:从对象页、架构、匹配规则到反向代理、负载均衡、TLS、缓存、流式连接和日志排障的学习入口。

#moc #growing #ops #nginx

[!info] related notes

Nginx MOC

知识总览

filters:
  and:
    - 'file.ext == "md"'
    - 'file.inFolder("z")'
    - 'this.file.links.contains(file)'
    - 'file.path != this.file.path'
formulas:
  knowledge: 'file.asLink(title)'
properties:
  formula.knowledge:
    displayName: 知识
  note.description:
    displayName: Description
  note.mastery:
    displayName: Mastery
views:
  - type: table
    name: 知识总览
    order:
      - formula.knowledge
      - note.description
      - note.mastery
    columnSize:
      formula.knowledge: 280
      note.description: 760
      note.mastery: 56

[!abstract] 学习目标 把 Nginx 理解为系统流量入口:它接收连接、选择 Server / Location、处理 TLS 和静态资源、代理到 Upstream,并通过 Buffering、Timeout、Load Balancing 和日志影响端到端行为。

一张图看请求路径

Client
→ listen / TLS / virtual server
→ location match + URI mapping
├─ static file
└─ proxy_pass → upstream group → application
→ response buffering / streaming
→ access and error logs

推荐阅读顺序

  1. Nginx:定位与适用场景。
  2. 正向代理与反向代理。
  3. Nginx 架构与请求处理流程:Master / Worker、事件驱动与连接处理。
  4. 安装与基础配置。
  5. location、root、alias 与 proxy_pass。
  6. 反向代理、负载均衡与网关入口。
  7. Buffering、Timeout、Cache 与日志排障。
  8. SSE 连接缓存与压缩问题。

五个核心模块

1. 进程与事件模型

Master → config / lifecycle / workers
Workers → event-driven, non-blocking connection handling

Nginx 擅长大量 I/O 连接,不代表上游应用和数据库也能承受同样并发。

2. Server 与 Location 匹配

请求先选择 server,再按 Prefix、Regex 等规则匹配 location。配置排障应先确认“实际命中了哪一段”,再检查代理和路径。

3. URI 与文件/代理映射

最常见错误来自:

location prefix
+ root vs alias
+ proxy_pass URI part
+ trailing slash

不要凭直觉修改斜杠,使用实际 URI 示例推导最终文件路径或 Upstream Path。

4. 反向代理、负载均衡与 TLS

  • Reverse Proxy:代表 Client 调用后端;
  • Load Balancing:在多个 Upstream 实例之间选择;
  • TLS Termination:在入口完成证书和加密,再向内部服务转发;
  • Gateway:在代理基础上增加认证、限流、路由和治理,但业务授权仍属于应用层。

5. Buffering、Timeout 与 Streaming

normal response
→ buffering may improve throughput and protect slow clients

SSE / streaming
→ excessive buffering delays visible output

Connect、Send、Read、Client Timeout 解决不同阶段,不能用一个巨大 Timeout 掩盖慢 Upstream。

常见状态码定位

状态常见方向
404Server / Location / URI / Static Path
413Client Body Size
499Client 在 Nginx 返回前断开
502无法连接 Upstream、协议或响应无效
504Upstream 在 Read Timeout 内未及时响应

状态码只是起点,继续关联 Access Log、Error Log、Upstream Timing 和应用 Trace。

流式与 WebSocket

  • SSE:关闭不必要 Buffering / Compression,保持 Heartbeat,处理代理 Idle Timeout;
  • WebSocket:正确转发 Upgrade / Connection,并设置适当 Read Timeout;
  • Client 断开后,应用应传播 Cancellation,避免后台继续生成内容。

面试掌握标准

  • 正向代理与反向代理;
  • Master / Worker 与事件驱动;
  • Server / Location 匹配;
  • Root、Alias、Proxy Pass 路径语义;
  • Upstream / Load Balancing / TLS Termination;
  • Buffering 与 Streaming 的冲突;
  • 502、504 和 499 的排障方向;
  • Nginx 与 API Gateway / Backend Authorization 的边界。

操作与排障入口

创建于 2025/1/1 更新于 2026/10/16
Digital Biome
Access

键盘导航

j / k / ↑ / ↓
选择对象
Enter / o
打开选择
/ 或 Ctrl+K
全站搜索
i / f / 1–4
筛选当前列表
g → h / l / p / t / a
首页 / 笔记 / 项目 / 工具 / About
r → 1–9
打开所选对象的关联
u
返回上一页
Esc
关闭浮层 / 取消命令;输入框内清空文本