这是《Nginx 从入门到精通》系列的第 15 篇(全系列 15 篇 + 4 个实战项目)。目标:把前 14 篇压成 16 张能复现的故障卡------拿到「502 / 413 / 串号 / 503 / 旧文件」这类现象后,能在一两分钟内定到该看哪一行日志、该改哪个默认值,并把每条修复落到监控或 CI 门禁上。
前置 :第 1 篇(进程模型与信号)、第 5 篇(反向代理与 upstream)、第 7 篇(
zone/resolve/摘流)、第 8 篇(缓存 key)、第 9 篇(证据链)、第 10 篇(会话复用与证书链)、第 12 篇(open_file_cache、buffer);if/map取舍见第 6 篇;实验复用lab/00-env(按需)。 本文所有数字与输出均为 nginx 1.30.6(stable)实测 ,对照nginx:1.24-alpine= 1.24.0;宿主端口一律127.0.0.1:19xxx,未改动lab/下任何文件。文末附自测 5 题与延伸阅读。
1. 先跑起来(5 分钟实验)
两张最便宜的卡------413 与 rewrite 死循环------一个容器、三条请求就能当场立案:
bash
mkdir -p /tmp/ch15 && cat > /tmp/ch15/mini.conf <<'NGINX'
worker_processes 1;
events { worker_connections 64; }
http { server { listen 8080;
location /a { rewrite ^ /b last; }
location /b { rewrite ^ /a last; }
} }
NGINX
docker run -d --name ch15-mini -p 127.0.0.1:19580:8080 -v /tmp/ch15/mini.conf:/etc/nginx/nginx.conf:ro nginx:stable-alpine
head -c 2000000 /dev/zero > /tmp/ch15/2m.bin; curl -s -o /dev/null -w 'upload=%{http_code}\n' --data-binary @/tmp/ch15/2m.bin localhost:19580/x
curl -s -o /dev/null -w 'rewrite=%{http_code}\n' localhost:19580/a
docker logs ch15-mini 2>&1 | grep -aE "too large|redirection cycle" && docker rm -f ch15-mini
| 命令 | 预期输出(实测) | 对应卡 |
|---|---|---|
2 MB POST → /x |
upload=413 |
卡 4(client_max_body_size 默认 1m) |
GET /a |
rewrite=500 |
卡 8(last 互踢成环) |
docker logs 抓两行 |
见下 | 这两行格式要背下来 |
text
[error] client intended to send too large body: 2000000 bytes, ... "POST /x HTTP/1.1"
[error] rewrite or internal redirection cycle while processing "/b", ... "GET /a HTTP/1.1"
五分钟结论:这两个故障不用读代码,error_log 一句就结案 。剩下 14 张卡要练的,是把「该看哪一行日志」变成肌肉记忆------这也是第 2 节那套读卡方法的全部意义。端口用 19580 是为了避开 lab/00-env 的 8080。
2. 怎么用这本案例库
2.1 一张卡的 5 段结构:缺一段就不算卡
| 段 | 要写什么 | 不合格的样子 |
|---|---|---|
| 现象 | 用户看到什么,带量化 (413、成功率掉一半、P99 阶梯上升) |
「偶发错误」「有时候慢」 |
| 证据 | 日志/抓包的原文,不是转述 | 「日志里报错了」 |
| 根因 | 指到具体指令 + 默认值(继承规则见第 3 篇) | 「配置有问题」 |
| 修复 | 可粘贴片段 + 版本下限 | 只给方向不给片段 |
| 防复发 | 监控项 / CI 门禁 / 演练动作 | 「注意一下」 |
2.2 证据阶梯:先看上面一层
排障顺序不是靠猜,而是按成本从低到高排;上层能回答的问题,绝不跳到下层:
| 层 | 命令 / 位置 | 能回答 | 成本 |
|---|---|---|---|
| 1 access log | $status $request_time $upstream_addr $upstream_cache_status |
是不是网关的锅、打到谁、缓存命中没 | 免费,常开 |
| 2 error_log + 连接/抓包 | /var/log/nginx/lab_error.log、stub_status、ss -s、tcpdump -i any -nn port 8080 -A |
上面那行没写出来的原因;TIME_WAIT 与连接数;上游到底回了什么字节 | 免费~要复现窗口 |
2.3 16 张卡索引(按现象分 6 类)
| 类 | 现象 | 卡 | 关键证据 |
|---|---|---|---|
| 上游连接 | 高 QPS 后上游连不上,ss -s 里 timewait 数万还在涨 |
卡 1 | 1.24.0 十个请求打出 conn#11;1.30.6 全是 conn#1,TIME_WAIT 为 0 |
| 上游连接 | 后端迁完、DNS 早指新 IP,网关仍打已摘流的旧 IP | 卡 7 | 静态 upstream 改 DNS 后仍是 app=be;变量形式 2 秒内自己变 app=be2 |
| 上游连接 | 新上 gRPC 经网关 100% 502,后端 HTTP/1.1 直连正常 | 卡 13 | upstream sent too large http2 frame: 4740180;上游侧抛 BrokenPipeError |
| 上游连接 | 流量不大但 Active connections 一路涨,谷时自愈 | 卡 14 | 8 条半开连接后 8 worker_connections are not enough,正常请求 normal=000 |
| 状态码 | 登录态用户偶发 502,匿名用户 100% 正常 | 卡 3 | upstream sent too big header ...;上游只多发了一个 12 KiB 的 Set-Cookie |
| 状态码 | 上传接口 413,后端明明允许 50 MB | 卡 4 | client intended to send too large body: 2000000 bytes;access log 413 183 |
| 状态码 | 某批 URL 稳定 500,改一行 rewrite 全站受影响 | 卡 8 | rewrite or internal redirection cycle while processing "/b" |
| 缓存 | 换账号后页面还是上一个人的数据 | 卡 2 | 带 Cookie: sess=bob 拿到 cookie=sess=alice,X-Cache: HIT |
| 缓存 | 某些客户端乱码或下载出 .gz,同 URL 在 curl 里正常 |
卡 11 | 不带编码的请求也回 Content-Encoding: gzip,体首 1f 8b 08 00 |
| TLS | HTTPS 高峰 CPU 打满、P99 阶梯上升,上游耗时没变 | 卡 5 | s_client -reconnect 全 New, TLSv1.2;日志 $ssl_session_reused=. |
| TLS | 浏览器/curl 正常,安卓老版本与某些 SDK 报证书无效 | 卡 16 | SSL certificate problem ...(exit=60);verify error:num=20 / num=21 |
| 配置语义 | 扫描报告「部分接口缺 X-Frame-Options」,首页有 /api/ 没有 |
卡 9 | /api/ 的 grep -ci x-frame 得 0,/page/ 得 1 |
| 配置语义 | if 里按条件分流时灵时不灵,或直接起不来 |
卡 10 | "proxy_pass" directive is not allowed here(server 级 if) |
| 配置语义 | 刚开 limit_req,正常用户开始吃 503 |
卡 12 | limiting requests, excess: 0.888 by zone "z1" |
| 生命周期 | reload 后 ps 长期有 worker process is shutting down |
卡 6 | 40 MB 大下载在途时 reload,新旧 worker 并存 |
| 生命周期 | 发版后仍返回旧文件,md5sum 是新的,过一分钟自己好 |
卡 15 | open_file_cache 开着时 mv 换 inode,仍返回 V1 |
2.4 八张展开卡(完整五段)
卡 1 · upstream 端口耗尽、TIME_WAIT 数万
- 现象 :QPS 到一定量级后上游连不上(
connect() failed (99: Cannot assign requested address)),ss -s里timewait数万还在涨。 - 证据 :同一条「10 请求」压测------
nginx:1.24.0无 keepalive →conn#10、conn#11(每请求一条新 TCP ,压 100 请求后容器内TIME_WAIT累计 230);nginx:1.30.6默认 → 10 行全conn#1(0 条新连接 、TIME_WAIT为 0)。复现见实验 1。 - 根因 :老版本默认
proxy_http_version 1.0,每请求新建上游 TCP;关闭方吃 60s×2 的 TIME_WAIT,本地端口被吃光。 - 修复 :upstream 加
keepalive 32;,location 加proxy_http_version 1.1; proxy_set_header Connection "";。≥1.29.7 上游 keep-alive 已默认开启,但仍要显式写才跨版本一致。 - 防复发 :监控
ss -s的 timewait 曲线;日志加$upstream_connect_time(恒为-/0 即命中池子);池大小按「峰值 QPS × 上游耗时」估(第 12 篇)。
卡 6 · reload 卡住、旧 worker 不退出
- 现象 :
nginx -s reload后ps长期有nginx: worker process is shutting down;反复 reload 后 worker 越积越多。 - 证据 :一个 40 MB 的大下载(
limit_rate 100k)在途时 reload,ps立刻出现worker process is shutting down(旧)与worker process(新)并存;另一组实测加worker_shutdown_timeout 2s;后第 2 秒worker process 30 exited with code 0,客户端curl exit=52(3.39 s 被切断)。复现见实验 4。 - 根因 :优雅退出要等存量连接自然结束,大文件/SSE/WS/慢上游会把「结束」拖到几分钟,而默认没有上限。
- 修复 :
worker_shutdown_timeout 10s;(≥1.11.11 ,main 上下文);配keepalive_timeout收空闲长连接;SSE 给proxy_read_timeout让流有终点。 - 防复发 :把 reload 变成演练项------发版前后各跑一次
ps+stub_status,worker 数回不到基线就告警。
卡 9 · 安全头部分接口缺失
- 现象 :扫描报告「部分接口缺 X-Frame-Options」;首页有、
/api/没有。 - 证据 :同站点
/page/有X-Frame-Options: SAMEORIGIN、X-Content-Type-Options: nosniff;/api/只剩Cache-Control: no-store------因为它在 location 里写了一行add_header;curl -sD- -o /dev/null .../api/ | grep -ci x-frame得到0。复现见实验 2。 - 根因 :
add_header是「要么全继承、要么全不继承 」:子级出现任意add_header,父级那一组全部失效(不是追加)。 - 修复 :常用头收进片段
include /etc/nginx/lab/conf.d/security-headers.conf;,每个写了add_header的 location 都 include 一次 ;统一带always让 4xx/5xx 也生效。 - 防复发:巡检脚本对每个对外 location 断言关键头存在,纳入 CI(第 14 篇)。
卡 11 · 缓存里存了 gzip 版本,切到不支持的客户端就乱码
- 现象 :某些客户端/老浏览器打开是乱码或下载出
.gz;同 URL 在 curl 里又正常。 - 证据 :先带
Accept-Encoding: gzip请求一次(上游回Content-Encoding: gzip且不发Vary),再不带任何头请求同 URL:X-Cache: HIT、Content-Encoding: gzip,响应体前 4 字节1f 8b 08 00(gzip 魔数)。复现见实验 2。 - 根因 :
proxy_cache_key不含$http_accept_encoding,上游又不发Vary,nginx 不区分编码,把压缩体发给了不接受 gzip 的客户端。 - 修复 :key 加编码维度(或用
map归一成gzip/none桶):proxy_cache_key "$scheme$request_method$host$request_uri$http_accept_encoding";;能改上游就让它发Vary: Accept-Encoding。纯 nginxgzip on不受影响(压缩发生在缓存之后)。 - 防复发:缓存 key 评审清单加「变体维度是否齐全」(编码/设备/语言/租户)。
卡 12 · 限流开启后正常用户大量 503
- 现象 :刚开
limit_req,正常用户开始吃 503,客服说「点一下要刷新」。 - 证据 :
rate=1r/s且limit_req zone=z1;(无 burst)连发 10 个:200 503 503 503 503 503 503 503 503 503;[error] limiting requests, excess: 0.888 by zone "z1"。改limit_req zone=z2 burst=5 delay=3; limit_req_status 429;后并发 20 →200/429混合,429 带Retry-After: 1。 - 根因 :
limit_req默认status是 503 、burst默认 0------没有任何突发余量,客户端重试/预取都撞墙;503 又让人误判「服务挂了」。 - 修复 :
limit_req_status 429;+limit_req_log_level warn;+ 合理burst(≈允许突发倍率×rate)+delay=(≥1.15.7 ,匀速放行);key 用$binary_remote_addr时记得叠业务维度。 - 防复发 :灰度开限流------先
limit_req_dry_run on(≥1.17.1)观察,再拦截;429 比例进看板。
卡 13 · gRPC 全 502 / upstream sent too large http2 frame
- 现象 :新上的 gRPC 服务经网关 100% 502,客户端报
UNAVAILABLE;后端 HTTP/1.1 直连却正常。 - 证据 :
[error] upstream sent too large http2 frame: 4740180 while reading response header from upstream, ... upstream: "grpc://127.0.0.1:8090",上游侧同时抛BrokenPipeError(它收到的是 HTTP/2 前导帧,不是 HTTP/1.1 请求行)。复现见实验 2。 - 根因 :
grpc_pass(≥1.13.10 )要求后端讲 h2c (HTTP/2 明文);指向只支持 HTTP/1.1 的服务,帧解析必然失败。另一高频坑是流式 RPC 被proxy_buffering on缓冲住。 - 修复 :后端开 h2c(Go
h2c.NewHandler、Java/Python 开 plaintext HTTP/2);grpc_pass grpc://app_pool;+grpc_read_timeout/grpc_send_timeout;流式接口proxy_buffering off;。 - 防复发 :灰度清单加 gRPC 冒烟(
grpcurl -plaintext list);$upstream_status/$upstream_addr进看板。
卡 14 · 慢速攻击把连接数打满,正常用户 503
- 现象 :流量不大但
stub_status的 Active connections 一路涨,正常用户超时/被重置,谷时自愈。 - 证据 :8 条只发半个请求头的连接(
worker_connections 8):[warn] 8 worker_connections are not enough, reusing connections、[alert] 8 worker_connections are not enough,正常curl直接normal=000;加client_header_timeout 5s后同批连接在 5 s 被记成"GET / HTTP/1.1" 408 0、正常请求 200;limit_conn perip 2下 2 个 6 s 在途请求占满配额,第 3 个 →503+[warn] limiting connections by zone "perip"。复现见实验 1。 - 根因 :
client_header_timeout/client_body_timeout默认 60s,攻击者几字节就能占住一条连接 60 秒;开源版只能靠超时 + 配额。 - 修复 :
client_header_timeout 10s; client_body_timeout 15s; keepalive_timeout 30s; lingering_close always;再叠limit_conn(它只统计已进入处理 的连接,挡不住半开连接),前面挂limit_req;真正的抗 D 交给 CDN/WAF。 - 防复发:定期跑慢速演练,验证 408 出现且 Active connections 回落;监控「408 比例」与「连接数/worker」。
卡 16 · 自家服务正常,部分客户端握手失败
- 现象:浏览器/curl 都正常,但安卓老版本、某些 HTTP SDK、小程序偶发「证书无效」。
- 证据 :只配叶证书时
curl→SSL certificate problem: unable to get local issuer certificate(exit=60);openssl s_client→verify error:num=20:unable to get local issuer certificate与num=21:unable to verify the first certificate;换成cat srv.crt int.crt > fullchain.crt后Verify return code: 0 (ok)。复现见实验 3。 - 根因 :服务器只发叶证书,客户端本地没有中间 CA(浏览器会自动补链,很多 SDK 不会);另外
ssl_protocols收敛过头会把只支持 TLS1.0/1.1 的老客户端挡在门外。 - 修复 :
ssl_certificate指 fullchain (叶 → 中间,不放根 );ssl_protocols TLSv1.2 TLSv1.3;并用客户端矩阵验证老端;OCSP stapling 用ssl_trusted_certificate(≥1.3.7)。 - 防复发 :证书巡检查链长与到期;发版后
testssl.sh回归(第 10 篇)。
2.5 八张紧凑卡(结论不删,证据在实验里)
- 卡 2 · 用户 A 看到用户 B 的数据 |现象:换账号后页面还是上一个人的数据,清浏览器缓存就好、过一会儿又串。证据:同一 URI 带
Cookie: sess=bob却拿到cookie=sess=alice,两次响应完全一致(没到上游,是缓存给的),X-Cache: HIT;同配置里 key 加了$cookie_sess的 location 就各拿各的。根因:proxy_cache_key默认是$scheme$proxy_host$request_uri,不含任何用户维度 ;上游又没发Vary/private。修复:二选一------key 加维度proxy_cache_key "$scheme$request_method$host$request_uri$cookie_session";,或个人化页面proxy_cache_bypass $cookie_session;/干脆不缓存。防复发:缓存 key 评审进 CR,缓存内容含用户态就必须带会话/租户维度,上线前用两个不同 Cookie 对打一次。 - 卡 3 · 502 upstream sent too big header |现象:登录态用户偶发 502,匿名用户 100% 正常,Cookie 一大就复现。证据:
[error] upstream sent too big header while reading response header from upstream,上游只多发了一个 12 KiB 的Set-Cookie;proxy_buffer_size 16k之后同一请求变 200。根因:proxy_buffer_size默认4k/8k(平台相关),它管的是响应头 ;头超过它 nginx 直接放弃回 502,不截断。修复:proxy_buffer_size 16k; proxy_buffers 8 16k; proxy_busy_buffers_size 32k;;治本是把 Cookie 拆小、少发Set-Cookie。防复发:压测必须带真实登录态(大 Cookie + SSO 头),502 时$upstream_header_time为-就是「头没读完」。完整解剖见第 4 节。 - 卡 4 · 上传大文件 413 |现象:上传接口 413,但后端明明允许 50 MB。证据:
[error] client intended to send too large body: 2000000 bytes, ... "POST /upload HTTP/1.1";access log"POST /upload HTTP/1.1" 413 183。根因:client_max_body_size默认 1m ,且 http/server/location 三级继承------写在http的 1m 会盖住你以为的「不限制」。修复:按路由放开location /api/upload { client_max_body_size 50m; };应用侧仍要留配额与磁盘水位。防复发:上传接口单测固定一条「超阈值大文件」,413 计入网关指标而非应用指标。(第 1 节已现场跑出upload=413,放开到20m后同一条变200。) - 卡 5 · TLS 握手 CPU 打满、P99 抖动 |现象:HTTPS 高峰 CPU 打满、P99 阶梯上升,而上游耗时没变。证据:TLS1.2、
openssl s_client -reconnect(首次 + 5 次重连,共 6 次握手):ssl_session_cache off; ssl_session_tickets off;→New, TLSv1.2×6;换ssl_session_cache shared:SSLCH15:10m→ 1 行New后 5 行Reused;日志$ssl_session_reused对应.与r。根因:全量握手 = 额外 1~2 RTT + 非对称运算;复用一关,每个新连接都付这份钱。修复:ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;(跨 worker 共享)+ tickets(TLS1.3 走 ticket,默认开);证书换 ECDSA 再省一截;ssl_early_data on(≥1.15.3 )只对幂等只读接口开。防复发:监控「握手数 vs 复用率」;证书/协议变更后跑testssl.sh回归(第 10 篇)。 - 卡 7 · 云上摘流后仍打旧 IP |现象:后端迁移后 DNS 早指向新 IP,网关仍往已摘流的旧 IP 打,成功率掉一半。证据:静态 upstream(域名写死)时把 DNS 答案从
172.31.15.10改成172.31.15.11,请求仍是app=be;同机resolver 172.31.15.30 valid=1s;+ 变量形式proxy_pass http://$up:8080在 2 秒内自己变成app=be2,无需 reload ;变量形式漏 resolver 则[error] no resolver defined to resolve ch15svc。根因:普通 upstream 的域名只在配置加载时解析一次 ;只有resolver+ 变量形式(或 ≥1.27.3 的server ... resolve+zone)才周期重解析。修复:lab 的做法是resolver 127.0.0.11 valid=5s ipv6=off;+upstream app_pool { zone app_pool 64k; server app1:8080 resolve ...; }(≥1.27.3,resolve必须配zone);变量形式补resolver_timeout 2s。防复发:摘流 SOP 是「先摘流 → 看$upstream_addr里旧 IP 消失 → 再下线」,up=进看板(第 7 篇)。 - 卡 8 · 500 + rewrite or internal redirection cycle |现象:某批 URL 稳定 500,改一行 rewrite 全站受影响。证据:
[error] rewrite or internal redirection cycle while processing "/b", ... "GET /a HTTP/1.1"------location /a { rewrite ^ /b last; }与location /b { rewrite ^ /a last; }互踢。根因:last会重走 location 匹配,环里每跳算一次内部跳转,撞到 nginx 的内部跳转上限就 500(该上限没有指令可调,治本是不写环);try_files回退到自身同理。修复:规则单向收敛------last换 location、break就地停;try_files回退目标必须是真实文件;条件分流用map而非 rewrite 链。防复发:CI 静态检查 rewrite/return 目标是否存在,灰度先 1% 流量。(第 1 节已跑出rewrite=500。) - 卡 10 · if 里写 proxy_pass 不生效/半生效 |现象:想在 location 里按条件分流,结果时灵时不灵,或者直接起不来。证据:三条实测
emerg------if 内proxy_set_header→"proxy_set_header" directive is not allowed here;server 级 if 内proxy_pass→"proxy_pass" directive is not allowed here;if 套 if →"if" directive is not allowed here。反过来,location 级 if 内proxy_pass能过nginx -t且实测可分流,但 if 分支里 Host 默认是上游组名(Host: be_if)。根因:location 里的if会生成隐式嵌套 location,可用指令集受限;官方 wiki IfIsEvil 的结论是 location 里只有return与rewrite ... last100% 安全。修复:分流用map算变量 + 多 location------map $arg_v $backend { default app_pool; "1" canary_pool; }+location / { proxy_pass http://$backend; };或拆成两个 location。防复发:CR 规则------location 内出现if必须写理由,if里只允许return/rewrite ... last/set。 - 卡 15 · 发版后一直返回旧文件 |现象:发版脚本把新文件放上去了,
md5sum是新的,但 curl/浏览器拿到旧内容,过一分钟自己好了。证据:open_file_cache max=1000 inactive=60s; open_file_cache_valid 60s;下用mv覆盖index.html(换 inode):请求仍返回V1;同机open_file_cache off的 server 立刻V2;nginx -s reload后前者也变V2。根因:open_file_cache缓存的是文件描述符与元信息 ;rename 式发版时旧 inode 仍被缓存持有,未过期就继续读旧文件(默认off,只有为性能开过的人才踩)。修复:rename 发版就把open_file_cache_valid调到秒级(如5s)或直接off;或在发布流程末尾强制 reload;与try_files/alias混用要单独验证。防复发:发布流水线固定「上传 → reload → 回读校验(curl 比对 md5)」三步,CDN 侧同步刷新。
2.6 怎么贡献一张新卡
写卡只有三条准入规则,缺一条就别提交:
- 必须能复现 :给出一段最小配置 + 一条命令,别人在
docker run起的干净容器里能跑出同一个状态码或同一行 error_log; - 证据必须是原文 :粘
error_log和 access log 的原始行,不写「报了个错」;数字(字节数、状态码、秒数)原样抄; - 修好不算完,得有防复发:这条卡进入哪一项监控、哪个 CI 门禁、哪次演练。没有防复发项的卡,三个月后一定复发。
3. 指令详解
左列「默认值」就是这 16 张卡撞墙的起点:先对默认值,再看坑。
| 指令 | 默认值 | 上下文 | 说明与坑 |
|---|---|---|---|
keepalive |
无(1.24 及以前不复用) | upstream | 上游连接池大小;<1.29.7 必须配 proxy_http_version 1.1 + Connection "" 才生效(卡 1) |
proxy_http_version |
1.0 |
http/server/location | 不改 1.1,上游 keepalive 永远不生效------「加了 keepalive 没效果」的第一原因 |
proxy_buffer_size / proxy_buffers |
4k/8k;8 4k/8 8k |
http/server/location | 前者只管响应头 (大 Cookie/SSO 头第一嫌疑),后者管响应体;流式接口要 proxy_buffering off(卡 3) |
client_max_body_size |
1m |
http/server/location | 三级继承,写在 http 的 1m 会盖住你以为的「不限制」(卡 4) |
client_header_timeout / client_body_timeout |
60s |
http/server/location | 慢速攻击靠它兜底:几字节就能占住一条连接 60 秒(卡 14) |
proxy_cache_key |
$scheme$proxy_host$request_uri |
http/server/location | 缺变体维度会串号(卡 2)或把 gzip 体发给不支持的客户端(卡 11) |
open_file_cache_valid |
60s |
http/server/location | 文件缓存过期时间与 rename 式发版冲突,旧 inode 未过期就继续读旧文件(卡 15) |
limit_req / limit_req_status |
无 / 503 |
http·server·location | 默认 503 会被误判宕机;配 burst + delay=(卡 12) |
limit_conn / limit_conn_status |
无 / 503 |
http·server·location | 只统计已进入处理的连接,挡不住半开连接(卡 14) |
worker_shutdown_timeout |
无(不限) | main | reload/停机时旧 worker 的硬上限(≥1.11.11),不设就是「等客户端」 (卡 6) |
ssl_session_cache |
none |
http/server | 只开 tickets 也能复用;跨 worker 共享用 shared:SSL:10m(卡 5) |
add_header |
无 | http/server/location/if | 子级一出现就整组覆盖父级(不是追加),统一 include + always(卡 9) |
grpc_pass |
无 | location·if | 后端必须 h2c(≥1.13.10),否则帧解析失败回 502(卡 13) |
4. 完整配置示例
卡 3 是「一个默认值 + 一行修复」的典型,拿它做完整解剖:同一个上游、同一个 curl,只改响应头缓冲三行,结果从 502 变 200。
先造一个「上游故意多发一个 12 KiB 响应头」的后端,/api/bigheader 回一个 12 KiB 的 Set-Cookie:
bash
mkdir -p /tmp/ch15/card3 && cd /tmp/ch15/card3
cat > big.py <<'PY'
from http.server import BaseHTTPRequestHandler, HTTPServer
BIG = "sess=" + "x" * 12288 # 约 12 KiB 的 Set-Cookie
class H(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Set-Cookie", BIG)
self.send_header("Content-Length", "2")
self.end_headers(); self.wfile.write(b"ok")
def log_message(self, *a): pass
HTTPServer(("0.0.0.0", 8080), H).serve_forever()
PY
报错版 bad.conf(响应头缓冲全走默认):
nginx
# bad.conf ------ 这就是「配置看起来没错、上游也没错」的 502
worker_processes 1;
events { worker_connections 64; }
http {
server {
listen 80;
location /api/ {
proxy_pass http://c3-up:8080/;
# 这里什么都不写:proxy_buffer_size 走默认 4k/8k
}
}
}
修复版 fixed.conf------与 bad.conf 只差带 ★ 的三行:
nginx
# fixed.conf ------ 三行 buffer 是全部改动
worker_processes 1;
events { worker_connections 64; }
http {
server {
listen 80;
location /api/ {
proxy_pass http://c3-up:8080/;
proxy_buffer_size 16k; # ★ 只管响应头,默认 4k/8k
proxy_buffers 8 16k; # ★ 响应体缓冲
proxy_busy_buffers_size 32k; # ★ 不设会被 nginx 报 "must be less than"
}
}
}
两版各跑一次,中间只换挂载的配置文件:
bash
docker network create --subnet 172.31.15.0/24 ch15net 2>/dev/null
docker run -d --name c3-up --network ch15net -v /tmp/ch15/card3/big.py:/srv/big.py:ro python:3-alpine python /srv/big.py
docker run -d --name c3-bad --network ch15net -p 127.0.0.1:19602:80 -v /tmp/ch15/card3/bad.conf:/etc/nginx/nginx.conf:ro nginx:stable-alpine
sleep 2; curl -s -o /dev/null -w 'bad=%{http_code}\n' localhost:19602/api/bigheader
docker logs c3-bad 2>&1 | grep -a "too big header"; docker rm -f c3-bad
docker run -d --name c3-fix --network ch15net -p 127.0.0.1:19602:80 -v /tmp/ch15/card3/fixed.conf:/etc/nginx/nginx.conf:ro nginx:stable-alpine
sleep 2; curl -s -o /dev/null -w 'fixed=%{http_code}\n' localhost:19602/api/bigheader
| 版本 | curl -w '%{http_code}' |
error_log |
|---|---|---|
bad.conf(默认缓冲) |
bad=502 |
[error] upstream sent too big header while reading response header from upstream, ... upstream: "http://172.31.15.10:8080/bigheader"(... 处是省略,你机器上是 c3-up 的容器 IP) |
fixed.conf(16k) |
fixed=200 |
没有这一行 |
相对 lab/00-env 改了哪几行 :/api/ 里新增 proxy_buffer_size 16k;、proxy_buffers 8 16k;、proxy_busy_buffers_size 32k; 三行,其余(listen 80 + 宿主 8080 映射、app_pool 三成员、lab_access.log/lab_error.log)与 lab 一致;本节为了最小复现把 upstream 换成了单成员的 c3-up:8080。收尾 docker rm -f c3-up c3-fix。
把 16 张卡的修复一次落进 lab 目录结构(/etc/nginx/lab/nginx.conf,容器用 -c 指定,已通过 nginx -t),改动只有这 8 处:
| # | 位置 | 改动 | 卡 |
|---|---|---|---|
| 1 | main | 新增 worker_shutdown_timeout 10s |
6 |
| 2 | http | 新增两个 *_zone + 两个 *_status、proxy_cache_path、map $http_accept_encoding |
12、14、2、11 |
| 3 | upstream app_pool |
新增 keepalive 32 / keepalive_requests / keepalive_timeout |
1 |
| 4 | server | 新增 client_max_body_size、两个超时、lingering_close、limit_conn |
4、14 |
| 5 | /api/ |
新增 limit_req、proxy_http_version + Connection ""、三行 buffer |
12、1、3 |
| 6 | 新增 /cached/ |
带维度 proxy_cache_key + X-Cache |
2、11 |
| 7 | log_format |
追加 cache= 与 ssl=$ssl_session_reused |
2、5、11 |
| 8 | 含 add_header 的 location |
统一 include security-headers.conf |
9 |
security-headers.conf 片段(供各 location include 一次):add_header X-Frame-Options "SAMEORIGIN" always; / add_header X-Content-Type-Options "nosniff" always; / add_header Referrer-Policy "strict-origin-when-cross-origin" always;。
5. 必做实验
四个实验覆盖 11 张卡,都在 127.0.0.1:19xxx 上跑,收尾 docker rm -f。预期输出全是实测值。
实验 1:连接层(卡 1 / 卡 14)
bash
mkdir -p /tmp/ch15
cat > /tmp/ch15/raw.py <<'PY'
import socket, threading
n=[0]
def h(c):
n[0]+=1; cid=n[0]; buf=b""
while True:
d=c.recv(4096)
if not d: break
buf+=d
while b"\r\n\r\n" in buf:
buf=buf.split(b"\r\n\r\n",1)[1]; print(f"conn#{cid}", flush=True)
c.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok")
s=socket.socket(); s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1); s.bind(("0.0.0.0",8080)); s.listen(128)
while True:
c,_=s.accept(); threading.Thread(target=h,args=(c,),daemon=True).start()
PY
printf 'events{worker_connections 8;}\nhttp{upstream r{server raw:8080;}\n server{listen 8080;location /{proxy_pass http://r;}}}\n' > /tmp/ch15/e1.conf
docker network create --subnet 172.31.15.0/24 ch15net 2>/dev/null
docker run -d --name raw --network ch15net --ip 172.31.15.20 -v /tmp/ch15/raw.py:/srv/raw.py:ro python:3-alpine python /srv/raw.py
docker run -d --name e1 --network ch15net --add-host raw:172.31.15.20 -p 127.0.0.1:19001:8080 -v /tmp/ch15/e1.conf:/etc/nginx/nginx.conf:ro nginx:stable-alpine
docker run -d --name e1-24 --network ch15net --add-host raw:172.31.15.20 -p 127.0.0.1:19002:8080 -v /tmp/ch15/e1.conf:/etc/nginx/nginx.conf:ro nginx:1.24-alpine
sleep 3; for i in $(seq 1 10); do curl -s -o /dev/null localhost:19001/; done; docker logs raw 2>&1 | tail -1
for i in $(seq 1 10); do curl -s -o /dev/null localhost:19002/; done; docker logs raw 2>&1 | tail -1
python3 -c "import socket,time;s=[socket.create_connection(('127.0.0.1',19001)) for _ in range(8)];[x.sendall(b'GET / HTTP/1.1\r\nHost: x\r\n') for x in s];time.sleep(20)" &
sleep 2; curl -s -o /dev/null -w 'normal=%{http_code}\n' localhost:19001/; docker logs e1 2>&1 | tail -1
| 观察点 | 预期输出(实测) | 说明 |
|---|---|---|
docker logs raw(1.30.6) |
10 行全是 conn#1------10 请求 0 条新连接 |
卡 1:≥1.29.7 上游 keep-alive 默认开启 |
| 同上(1.24.0) | conn#11------10 个请求用掉 10 条新连接 |
老版本必须显式配 keepalive |
| 8 条半开连接后 | [alert] 8 worker_connections are not enough,正常请求 normal=000 |
卡 14:默认 60s 超时,几字节就能占满连接 |
为什么 :假上游每读到一个完整请求就把连接编号打出来,编号变不变直接暴露「有没有复用 TCP」;而 8 条只发半个头的连接吃满 worker_connections 8,是因为 client_header_timeout 默认 60s------攻击者不需要流量,只需要时间。
实验 2:缓存与配置语义(卡 2 / 8 / 9 / 11 / 13)
bash
cat > /tmp/ch15/both.conf <<'NGINX'
events{worker_connections 256;}
http{
proxy_cache_path /tmp/c levels=1:2 keys_zone=bz:10m;
server{ listen 8090; gzip on; gzip_vary off; gzip_min_length 1; gzip_types text/plain;
location / { return 200 "cookie=$http_cookie\n"; } } # 上游故意不发 Vary
server{ listen 8080;
add_header X-Frame-Options "SAMEORIGIN" always;
location /cache-a { proxy_cache bz; proxy_cache_valid 200 10m; add_header X-Cache $upstream_cache_status always; proxy_pass http://127.0.0.1:8090; }
location /cache-c { proxy_cache bz; proxy_cache_valid 200 10m; add_header X-Cache $upstream_cache_status always; proxy_pass http://127.0.0.1:8090; }
location /cache-b { proxy_cache bz; proxy_cache_valid 200 10m; proxy_cache_key "$scheme$host$uri$cookie_sess"; add_header X-Cache $upstream_cache_status always; proxy_pass http://127.0.0.1:8090; }
location /api/ { add_header Cache-Control "no-store"; proxy_pass http://127.0.0.1:8090/; }
location /page/ { return 200 "page\n"; }
location /loop-a { rewrite ^ /loop-b last; } location /loop-b { rewrite ^ /loop-a last; }
}
server{ listen 8081; location / { grpc_pass grpc://127.0.0.1:8090; } }
}
NGINX
docker run -d --name both -p 127.0.0.1:19003:8080 -p 127.0.0.1:19005:8081 -v /tmp/ch15/both.conf:/etc/nginx/nginx.conf:ro nginx:stable-alpine
curl -s -H 'Cookie: sess=alice' localhost:19003/cache-a; curl -s -H 'Cookie: sess=bob' localhost:19003/cache-a # 卡2:bob 拿到 alice 的
curl -s -H 'Cookie: sess=bob' localhost:19003/cache-b # 各自正确
curl -sD- -o /dev/null -H 'Accept-Encoding: gzip' localhost:19003/cache-c | grep -iE "x-cache|content-encoding"
curl -sD- -o /tmp/ch15/gz.bin localhost:19003/cache-c | grep -iE "x-cache|content-encoding"; od -An -tx1 -N4 /tmp/ch15/gz.bin # 卡11:1f 8b 08 00
curl -sD- -o /dev/null localhost:19003/api/ | grep -ci x-frame; curl -sD- -o /dev/null localhost:19003/page/ | grep -ci x-frame # 卡9:0 与 1
curl -s -o /dev/null -w 'rewrite=%{http_code}\n' localhost:19003/loop-a # 卡8:500
curl -s -o /dev/null -w 'grpc=%{http_code}\n' localhost:19005/ # 卡13:502
docker logs both 2>&1 | grep -aE "http2 frame|redirection cycle" | tail -2
| 观察点 | 预期输出(实测) | 说明 |
|---|---|---|
两用户打 /cache-a |
cookie=sess=alice 两次(第二次 X-Cache: HIT) |
卡 2:默认 key 缺 cookie 维度 |
两用户打 /cache-b |
sess=alice / sess=bob 各拿各的 |
key 加 $cookie_sess 后正确 |
先 gzip 后无编码打 /cache-c |
两次都 Content-Encoding: gzip,第二次 X-Cache: HIT,体首 1f 8b 08 00 |
卡 11:压缩体发给了不支持压缩的客户端 |
/api/ vs /page/ 的 x-frame 计数 |
0 / 1 |
卡 9:location 里那行 add_header 覆盖了父级整组 |
/loop-a 与 8081 |
rewrite=500 + rewrite or internal redirection cycle;grpc=502 + upstream sent too large http2 frame: 4740180 |
卡 8、卡 13 |
为什么 :/cache-a 与 /cache-b 用同一个 keys_zone,唯一的差别是 proxy_cache_key------命中率一样高、结果一个串号一个不串,说明问题不在缓存本身而在 key 的维度。gzip 那组同理:gzip_vary off 的上游不发 Vary,nginx 只能靠 key 区分编码,key 里没有编码维度,压缩体就发给了所有人。
实验 3:TLS(卡 5 / 卡 16)
bash
mkdir -p /tmp/ch15/tls && cd /tmp/ch15/tls
openssl req -x509 -newkey rsa:2048 -nodes -days 3650 -subj "/CN=Ch15 Root" -keyout ca.key -out ca.crt
openssl req -newkey rsa:2048 -nodes -subj "/CN=Ch15 Int" -keyout int.key -out int.csr
printf "basicConstraints=critical,CA:TRUE\nkeyUsage=critical,keyCertSign\n" > int.ext && openssl x509 -req -in int.csr \
-CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -extfile int.ext -out int.crt
openssl req -newkey rsa:2048 -nodes -subj "/CN=localhost" -keyout srv.key -out srv.csr
printf "subjectAltName=DNS:localhost,IP:127.0.0.1\n" > srv.ext && openssl x509 -req -in srv.csr \
-CA int.crt -CAkey int.key -CAcreateserial -days 825 -extfile srv.ext -out srv.crt
cat srv.crt int.crt > fullchain.crt # 卡16 的修复就是这一行
cat > /tmp/ch15/tls.conf <<'NGINX'
events{worker_connections 128;}
http{
server{ listen 8441 ssl; ssl_certificate /etc/nginx/tls/srv.crt; ssl_certificate_key /etc/nginx/tls/srv.key; location /{return 200 "leaf\n";} }
server{ listen 8442 ssl; ssl_certificate /etc/nginx/tls/fullchain.crt; ssl_certificate_key /etc/nginx/tls/srv.key; location /{return 200 "chain\n";} }
server{ listen 8443 ssl; ssl_protocols TLSv1.2; ssl_session_cache off; ssl_session_tickets off;
ssl_certificate /etc/nginx/tls/fullchain.crt; ssl_certificate_key /etc/nginx/tls/srv.key; location /{return 200 "off\n";} }
server{ listen 8444 ssl; ssl_protocols TLSv1.2; ssl_session_cache shared:SSLCH15:10m;
ssl_certificate /etc/nginx/tls/fullchain.crt; ssl_certificate_key /etc/nginx/tls/srv.key; location /{return 200 "reuse\n";} }
}
NGINX
docker run -d --name tls -p 127.0.0.1:19006:8441 -p 127.0.0.1:19007:8442 -p 127.0.0.1:19008:8443 -p 127.0.0.1:19009:8444 \
-v /tmp/ch15/tls:/etc/nginx/tls:ro -v /tmp/ch15/tls.conf:/etc/nginx/nginx.conf:ro nginx:stable-alpine
sleep 2
curl -s --cacert ca.crt https://localhost:19006/; echo "leaf exit=$?"; curl -s --cacert ca.crt https://localhost:19007/; echo "chain exit=$?"
echo | openssl s_client -connect 127.0.0.1:19006 -CAfile ca.crt 2>&1 | grep -E "verify error" | head -2 # 卡16:num=20/21
echo | openssl s_client -connect 127.0.0.1:19008 -tls1_2 -reconnect 2>&1 | grep -cE "^New," # 卡5:全 New(6)
echo | openssl s_client -connect 127.0.0.1:19009 -tls1_2 -reconnect 2>&1 | grep -E "^(New|Reused)," | head -6 # 卡5:首行 New 后 5 行 Reused
| 观察点 | 预期输出(实测) | 说明 |
|---|---|---|
| 只配叶证书 | leaf exit=60 + SSL certificate problem: unable to get local issuer certificate;openssl 报 verify error:num=20 / num=21 |
卡 16:缺中间证书 |
| fullchain / cache+tickets off | chain + chain exit=0;grep -c "^New," 得 6(全部 New) |
修复验证;卡 5 的 $ssl_session_reused=. |
换 shared:SSLCH15:10m |
首行 New,其后 Reused |
卡 5:日志 reused=r |
为什么 :叶证书由中间 CA 签发,客户端只拿到叶就断链------浏览器会自动下载缺的中间证书,很多 SDK 不会,curl --cacert 也不会,所以「浏览器正常」从来不算证据。会话复用看的是 -reconnect 的第 2 次握手:New 表示重做了一次完整握手(1~2 RTT + 非对称运算),Reused 表示命中了 shared 缓存。
实验 4:生命周期(卡 6 / 卡 15)
bash
mkdir -p /tmp/ch15/www && head -c 40000000 /dev/urandom > /tmp/ch15/www/big.bin && printf 'V1\n' > /tmp/ch15/www/index.html
cat > /tmp/ch15/life.conf <<'NGINX'
worker_processes 1; # 加一行 worker_shutdown_timeout 2s; 就是卡6 的修复
events{worker_connections 128;}
http{
server{ listen 8080; root /usr/share/nginx/html; location / { limit_rate 100k; } } # 40MB@100k ≈ 400s
server{ listen 8081; root /usr/share/nginx/html;
open_file_cache max=1000 inactive=60s; open_file_cache_valid 60s; location / { } }
}
NGINX
docker run -d --name life -p 127.0.0.1:19010:8080 -p 127.0.0.1:19011:8081 \
-v /tmp/ch15/www:/usr/share/nginx/html:ro -v /tmp/ch15/life.conf:/etc/nginx/nginx.conf:ro nginx:stable-alpine
sleep 2; curl -s -o /dev/null --max-time 8 localhost:19010/big.bin & # 大下载在途,客户端 8s 后放弃
sleep 2; docker exec life nginx -s reload; sleep 1
docker exec life ps -o pid,etime,args | grep nginx # 旧 worker 在 shutting down
sleep 6; curl -s localhost:19011/index.html # 预热 open_file_cache:V1
printf 'V2\n' > /tmp/ch15/www/.new && mv /tmp/ch15/www/.new /tmp/ch15/www/index.html
curl -s localhost:19011/index.html # 卡15:仍是 V1
docker exec life nginx -s reload; sleep 1; curl -s localhost:19011/index.html # V2
| 观察点 | 预期输出(实测) | 说明 |
|---|---|---|
reload 后 1s 的 ps |
两行 worker:nginx: worker process is shutting down 与 nginx: worker process |
卡 6:旧 worker 等存量请求 |
| 下载在途时 | 两个 worker 并存(旧 is shutting down),客户端 8 s 放弃后旧 worker 才退出、回到 1 个;另一组加 worker_shutdown_timeout 2s 的实测:第 2 秒 worker process 30 exited with code 0、客户端 curl exit=52(3.39 s 被切断) |
没有上限时等多久取决于客户端;有上限就是「切断」,发版前确认业务能接受 |
| rename 换文件 | 开 open_file_cache 的 19011 → V1;reload 后 → V2 |
卡 15:缓存的是 inode 不是路径 |
为什么 :reload 只给 master 发 HUP,旧 worker 要服务完存量连接才退------40 MB 在 limit_rate 100k 下约 400 秒,所以「reload 卡住」其实是「连接没有终点」。而 mv 覆盖是换 inode:open_file_cache 手里握着旧 inode 与它的元信息,路径没变、缓存没过期,读到的就还是旧内容。
收尾:docker rm -f raw e1 e1-24 both tls life; docker network rm ch15net。
6. 验收自测(5 题)
先盖住答案要点自己写一遍,再逐条对照;答不上来的回对应卡重读。
Q1. 接口偶发 502,error.log 只有 upstream sent too big header while reading response header from upstream。先动哪个指令?为什么不是 proxy_buffers?
答案要点:先动
proxy_buffer_size------它管响应头 ,默认4k/8k;proxy_buffers管响应体,对「头部超限」无效,这是最常见误修。成对写法是proxy_buffer_size 16k; proxy_buffers 8 16k; proxy_busy_buffers_size 32k;,同时从源头缩 Cookie、少发Set-Cookie(第 4 节有完整对照)。
Q2. 缓存上线后串号,命中率 90%+ 且上游零请求。给两个最小修复,并说明为什么「清缓存」不算修复。
答案要点:①
proxy_cache_key加会话/租户维度(如$cookie_session);②个人化页面proxy_cache_bypass $cookie_session;或不缓存。清缓存只抹掉本次脏数据,key 不变下一刻照样串------所以防复发要落成「缓存 key 评审进 CR,上线前用两个不同 Cookie 对打一次」。
Q3. 已把 limit_req_status 改成 429 并加了 burst=20,压测仍有正常用户失败。最可能漏了哪两件事?
答案要点:①
delay=(不设 delay,burst 内仍瞬间放行,节奏没被拉平);②key 粒度------$binary_remote_addr会把整栋楼 NAT 出口算成一个用户,应叠$server_name/$http_authorization。灰度期先limit_req_dry_run on(≥1.17.1)观察再拦截。
Q4. reload 后 ps 长期有 worker process is shutting down,怎么判断是正常等待还是出事?怎么给上限?
答案要点:看
stub_status的 Active connections 是否下降、长耗时是否集中在 SSE/大文件/慢上游;不降说明有连接没有终点。上限用worker_shutdown_timeout 10s;(≥1.11.11,main 上下文),并给 SSE 的proxy_read_timeout明确值------有上限就意味着到点切断,发版前要确认业务能接受。
Q5. 同域名浏览器正常、某安卓 SDK 报证书无效。给出三步检查顺序。
答案要点:①
openssl s_client -connect host:443 -servername host看链长与Verify return code(缺中间证书报num=20/num=21);②确认ssl_certificate指 fullchain、顺序「叶 → 中间」、不含根 ;③看ssl_protocols是否挡掉老客户端(TLSv1.2 TLSv1.3会挡住只支持 TLS1.0/1.1 的老端)。注意「浏览器正常」不算证据。
7. 常见坑
- 加了
keepalive 32但$upstream_connect_time不降 。根因:缺proxy_http_version 1.1与Connection ""。正解:三条一起写;<1.29.7 必须显式,≥1.29.7 也别省(跨版本一致)。 - 只调
proxy_buffers治不好too big header。根因:看错 buffer(头 vs 体)。正解:改proxy_buffer_size,同时把 Cookie 缩下来。 add_header加了always仍不生效 。根因:该 location 里还有别的add_header,父级整组失效。正解:用include片段,每个写了add_header的 location 都重申一次。resolver加了仍打旧 IP 。根因:用的是不带变量的静态 upstream,域名只在配置加载时解析一次。正解:server ... resolve+zone(≥1.27.3),或改用变量形式proxy_pass http://$up:8080。limit_conn挡不住慢速攻击 。根因:半开连接不进入统计。正解:client_header_timeout是第一道闸(默认 60s 太长),limit_conn只是第二道。- 发版后 1 分钟内偶发旧内容 。根因:
open_file_cache缓存了 inode。正解:open_file_cache_valid 5s或发版后强制 reload,并在流水线里做「上传 → reload → curl 比对 md5」。 nginx -t过了但线上 502 。根因:语法 ≠ 上游可达,-t不查 DNS 与健康状态。正解:proxy_connect_timeout 2s快速失败 + 发版后打/healthz。- 改了配置没生效,反复怀疑语法 。根因:文件改了,进程没重载。正解:固定
nginx -t && nginx -s reload,并用ps核对 master 的启动时间。
8. 延伸阅读
| 文档页 | 解决什么问题 |
|---|---|
| ngx_http_proxy_module · proxy_buffer_size | 卡 3:响应头缓冲与 proxy_buffers/proxy_busy_buffers_size 的官方定义与约束 |
| ngx_http_upstream_module · keepalive | 卡 1:上游连接池、keepalive_requests、server ... resolve 与 zone 的关系 |
| ngx_http_core_module · open_file_cache | 卡 15:文件缓存与 rename 式发版的冲突;同页可查 client_header_timeout、worker_shutdown_timeout |
| ngx_http_ssl_module · ssl_session_cache | 卡 5/卡 16:会话复用、ssl_session_tickets、证书链与 ssl_trusted_certificate |
| ngx_http_limit_req_module | 卡 12:burst/delay/limit_req_status/limit_req_dry_run 的精确语义 |
| ngx_http_rewrite_module | 卡 8/卡 10:last/break 与内部跳转上限,if 的合法用法 |
两条补充(站外但值得读一次):官方博客说明上游 keep-alive 自 1.29.7 起默认开启 blog.nginx.org/blog/keep-a...;卡 10 的出处是 "If is Evil" www.nginx.com/resources/w...。
章节依赖 :本篇是前 14 篇的收口------第 5、7 篇给上游连接与 zone/resolve,第 8 篇给缓存 key,第 9 篇给证据链,第 10 篇给证书链与会话复用,第 12 篇给 open_file_cache 与 buffer;反过来,这 16 张卡也是前面每一章「默认值 + 继承规则」的验收清单,建议在读完对应章节后回本库对撞一次。
系列导航:第 1 篇进程模型 → 第 2 篇安装升级 → 第 3 篇配置语法 → 第 4 篇静态站与 location → 第 5 篇反向代理 → 第 6 篇 rewrite 与 server_name → 第 7 篇负载均衡进阶 → 第 8 篇缓存 → 第 9 篇日志排障 → 第 10 篇 HTTPS 与安全 → 第 11 篇 Lua 与 OpenResty → 第 12 篇性能调优 → 第 13 篇高可用架构与网关 → 第 14 篇可观测性与工程化 → 第 15 篇(本篇)故障案例库