738 lines
74 KiB
HTML
738 lines
74 KiB
HTML
|
|
<!DOCTYPE html>
|
|||
|
|
<html lang="zh-CN" data-theme="light">
|
|||
|
|
<head>
|
|||
|
|
<meta charset="utf-8">
|
|||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|||
|
|
<title>Nginx 专题知识库 · 从开发联调到生产运维</title>
|
|||
|
|
<meta name="description" content="Nginx 全知识点专题:17 章覆盖架构与进程模型、配置与 location 匹配、静态服务、反向代理、负载均衡、HTTPS、rewrite 与变量、限流与访问控制、缓存、日志、性能调优、生产排障与开发实战,关键结论均在 nginx 1.27.5 上实测核验。">
|
|||
|
|
<meta name="keywords" content="Nginx 反向代理 负载均衡 location proxy_pass 限流 HTTPS 性能调优 排障 502">
|
|||
|
|
<link rel="stylesheet" href="assets/doc.css">
|
|||
|
|
<style>
|
|||
|
|
/* ===== 目录页专属样式(章节页不使用) ===== */
|
|||
|
|
.hero{background:
|
|||
|
|
radial-gradient(900px 240px at 90% -30%,rgba(0,150,57,.20),transparent),
|
|||
|
|
radial-gradient(700px 220px at 4% -40%,rgba(59,189,99,.16),transparent),
|
|||
|
|
var(--card);
|
|||
|
|
border:1px solid var(--line);border-radius:18px;padding:26px 28px 22px;box-shadow:var(--shadow);margin:0 0 20px}
|
|||
|
|
.hero h1{margin:0 0 8px;font-size:34px;letter-spacing:-.4px;line-height:1.3}
|
|||
|
|
.hero h1 em{font-style:normal;background:linear-gradient(135deg,#009639,#3bbd63);-webkit-background-clip:text;
|
|||
|
|
background-clip:text;-webkit-text-fill-color:transparent}
|
|||
|
|
.hero p{margin:0 0 12px;color:var(--muted);font-size:16.6px;max-width:96ch}
|
|||
|
|
.kpis{display:flex;gap:10px;flex-wrap:wrap;margin-top:8px}
|
|||
|
|
.kpi{border:1px solid var(--line);background:var(--soft);border-radius:12px;padding:10px 15px;min-width:120px}
|
|||
|
|
.kpi b{display:block;font-size:23px;font-variant-numeric:tabular-nums;line-height:1.3}
|
|||
|
|
.kpi span{font-size:13.8px;color:var(--muted)}
|
|||
|
|
.toolbar{display:flex;gap:9px;align-items:center;flex-wrap:wrap;margin:16px 0 0;padding-top:14px;border-top:1px dashed var(--line)}
|
|||
|
|
.toolbar .search{position:relative}
|
|||
|
|
.toolbar .search input{font:inherit;font-size:15.6px;padding:9px 13px 9px 34px;border:1px solid var(--line);
|
|||
|
|
border-radius:10px;background:var(--card);color:var(--ink);outline:none;width:340px;max-width:60vw}
|
|||
|
|
.toolbar .search input:focus{border-color:var(--brand);box-shadow:0 0 0 3px var(--brand-soft)}
|
|||
|
|
.toolbar .search::before{content:"⌕";position:absolute;left:12px;top:50%;transform:translateY(-52%);
|
|||
|
|
color:var(--muted);font-size:17px}
|
|||
|
|
.progwrap{display:flex;align-items:center;gap:9px;margin-left:auto}
|
|||
|
|
.progwrap .bar{width:132px;height:7px;border-radius:999px;background:var(--soft);border:1px solid var(--line);overflow:hidden}
|
|||
|
|
.progwrap .bar i{display:block;height:100%;width:0;border-radius:999px;background:linear-gradient(90deg,#009639,#3bbd63);transition:width .3s}
|
|||
|
|
.progwrap b{font-size:15px;font-variant-numeric:tabular-nums;min-width:40px;text-align:right}
|
|||
|
|
|
|||
|
|
section.block{margin-top:22px;background:var(--card);border:1px solid var(--line);border-radius:16px;
|
|||
|
|
padding:22px 24px;box-shadow:var(--shadow);scroll-margin-top:80px}
|
|||
|
|
section.block > h2{margin-top:0}
|
|||
|
|
section.block > .sub{margin:-6px 0 16px;font-size:15.6px;color:var(--muted)}
|
|||
|
|
.empty{border:1px dashed var(--line);border-radius:12px;padding:16px 18px;color:var(--muted);background:var(--soft)}
|
|||
|
|
|
|||
|
|
.map{display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:12px}
|
|||
|
|
.map .grp{border:1px solid var(--line);border-radius:13px;padding:14px 16px;background:var(--soft)}
|
|||
|
|
.map .grp h4{margin:0 0 4px;font-size:16.6px}
|
|||
|
|
.map .grp p{margin:0 0 9px;font-size:14.8px;color:var(--muted)}
|
|||
|
|
.map .grp .lk{display:flex;flex-wrap:wrap;gap:6px}
|
|||
|
|
.map .grp a{font-size:14.4px;border:1px solid var(--line);background:var(--card);border-radius:999px;
|
|||
|
|
padding:3px 10px;color:var(--muted);white-space:nowrap}
|
|||
|
|
.map .grp a:hover{border-color:var(--brand);color:var(--brand);text-decoration:none}
|
|||
|
|
|
|||
|
|
.chapcard{border:1px solid var(--line);border-radius:14px;padding:15px 18px;margin-bottom:11px;background:var(--soft);
|
|||
|
|
transition:border-color .15s}
|
|||
|
|
.chapcard:hover{border-color:color-mix(in srgb,var(--brand) 45%,var(--line))}
|
|||
|
|
.chapcard.done-shown{border-color:color-mix(in srgb,var(--ok) 45%,var(--line));
|
|||
|
|
background:color-mix(in srgb,var(--ok) 5%,var(--card))}
|
|||
|
|
.chapcard.hide{display:none}
|
|||
|
|
.cc-head{display:flex;align-items:flex-start;gap:11px}
|
|||
|
|
.cc-head input[type=checkbox]{margin:4px 0 0;width:19px;height:19px;accent-color:var(--ok);cursor:pointer;flex:0 0 auto}
|
|||
|
|
.cc-no{display:inline-grid;place-items:center;min-width:30px;height:30px;padding:0 8px;border-radius:8px;
|
|||
|
|
background:var(--brand-soft);color:var(--brand);font-size:14.4px;font-weight:800;flex:0 0 auto}
|
|||
|
|
.cc-title{font-size:19px;font-weight:700;color:var(--ink);flex:1;min-width:200px}
|
|||
|
|
.cc-title:hover{color:var(--brand);text-decoration:none}
|
|||
|
|
.cc-go{font-size:14.6px;font-weight:700;color:var(--brand);white-space:nowrap;margin-top:2px}
|
|||
|
|
.cc-desc{margin:8px 0 0 41px;font-size:15.6px;color:var(--muted)}
|
|||
|
|
.cc-secs{margin:9px 0 0 41px;font-size:15px;color:var(--muted)}
|
|||
|
|
.cc-secs summary{cursor:pointer;color:var(--brand);font-size:14.6px;font-weight:600}
|
|||
|
|
.cc-secs div{margin-top:8px;line-height:2}
|
|||
|
|
.cc-secs a{color:var(--muted);border-bottom:1px solid var(--line);margin-right:13px;display:inline-block}
|
|||
|
|
.cc-secs a:hover{color:var(--brand);border-bottom-color:var(--brand);text-decoration:none}
|
|||
|
|
|
|||
|
|
.grid2{display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:12px}
|
|||
|
|
.grid2 .box{margin:0}
|
|||
|
|
.foot{margin-top:26px}
|
|||
|
|
@media (max-width:1080px){
|
|||
|
|
.hero h1{font-size:26px}
|
|||
|
|
.cc-desc,.cc-secs{margin-left:0}
|
|||
|
|
.progwrap{width:100%;margin-left:0}
|
|||
|
|
}
|
|||
|
|
</style>
|
|||
|
|
</head>
|
|||
|
|
<body>
|
|||
|
|
|
|||
|
|
<header class="topbar">
|
|||
|
|
<div class="tb-in">
|
|||
|
|
<a class="brand" href="index.html"><span class="logo">NGX</span><div>Nginx 专题<small>从开发联调到生产运维</small></div></a>
|
|||
|
|
<div class="spacer"></div>
|
|||
|
|
<button class="btn" id="tocToggle">☰ 章节</button>
|
|||
|
|
<button class="btn" id="themeBtn" title="切换到深色主题">深色</button>
|
|||
|
|
</div>
|
|||
|
|
</header>
|
|||
|
|
|
|||
|
|
<div class="layout" id="layout">
|
|||
|
|
<aside class="sidebar">
|
|||
|
|
<h2>章节目录</h2>
|
|||
|
|
<nav id="chapters"><a class="toc-a" href="01-what.html"><i class="cn">1</i>Nginx 全景与选型</a><a class="toc-a" href="02-install.html"><i class="cn">2</i>安装、目录布局与运行控制</a><a class="toc-a" href="03-config.html"><i class="cn">3</i>配置结构、指令继承与 location 匹配</a><a class="toc-a" href="04-arch.html"><i class="cn">4</i>进程模型与请求处理阶段:nginx 为什么快、指令什么时候生效</a><a class="toc-a" href="05-static.html"><i class="cn">5</i>静态资源服务与缓存头</a><a class="toc-a" href="06-proxy.html"><i class="cn">6</i>反向代理核心:proxy_pass 的完整语义</a><a class="toc-a" href="07-lb.html"><i class="cn">7</i>负载均衡与上游健康管理</a><a class="toc-a" href="08-tls.html"><i class="cn">8</i>HTTPS / TLS 与证书治理</a><a class="toc-a" href="09-rewrite.html"><i class="cn">9</i>变量、rewrite 与进阶路由</a><a class="toc-a" href="10-security.html"><i class="cn">10</i>访问控制、限流限速与安全加固</a><a class="toc-a" href="11-cache.html"><i class="cn">11</i>缓存体系:proxy_cache 与微缓存</a><a class="toc-a" href="12-log.html"><i class="cn">12</i>日志体系与可观测性:让 502 自己说出原因</a><a class="toc-a" href="13-perf.html"><i class="cn">13</i>性能调优与协议演进:先测量,再改一个变量</a><a class="toc-a" href="14-ops.html"><i class="cn">14</i>生产运维、发布与排障手册</a><a class="toc-a" href="15-devtools.html"><i class="cn">15</i>开发场景实战:把 Nginx 用进日常</a><a class="toc-a" href="16-cheat.html"><i class="cn">16</i>速查表与高频坑</a><a class="toc-a" href="17-checklist.html"><i class="cn">17</i>能力自测、面试问答与继续深入</a></nav>
|
|||
|
|
<div class="side-foot">
|
|||
|
|
<a class="btn" href="#chapters-list">章节目录</a>
|
|||
|
|
<a class="btn" href="#start">5 分钟上手</a>
|
|||
|
|
<a class="btn" href="#pitfalls">最常见的坑</a>
|
|||
|
|
<a class="btn" href="../docker/index.html">Docker 专题 ↗</a>
|
|||
|
|
<a class="btn" href="../linux/index.html">Linux 专题 ↗</a>
|
|||
|
|
</div>
|
|||
|
|
</aside>
|
|||
|
|
<div class="mask" id="mask"></div>
|
|||
|
|
|
|||
|
|
<main class="main-col">
|
|||
|
|
<article>
|
|||
|
|
<section class="hero">
|
|||
|
|
<h1>Nginx 专题:<em>从一份 nginx.conf 到一套能扛流量的入口层</em></h1>
|
|||
|
|
<p>这不是指令字典,而是一份<b>按「你真正会被卡住的顺序」组织</b>的 Nginx 知识库:
|
|||
|
|
先在本地把前后端联调跑通,再把代理、HTTPS、负载均衡配对,最后按生产标准做限流、
|
|||
|
|
缓存、日志、性能与排障。每章都回答三件事——<b>什么时候会用到、配错会怎样、怎么验证</b>。</p>
|
|||
|
|
<p>本站最不一样的地方:那些最容易写错的结论(location 到底谁优先、
|
|||
|
|
<code>proxy_pass</code> 带不带斜杠差在哪、<code>add_header</code> 为什么不继承、
|
|||
|
|
同 location 里的 <code>return</code> 会不会绕过 <code>deny</code>),
|
|||
|
|
全部在真实容器里跑过一遍,<b>结论都带实测证据</b>。</p>
|
|||
|
|
<div class="kpis" id="kpis"><!-- AUTO:kpis -->
|
|||
|
|
<div class="kpi"><b>17</b><span>章 · 完整知识体系</span></div>
|
|||
|
|
<div class="kpi"><b>143</b><span>个知识点小节</span></div>
|
|||
|
|
<div class="kpi"><b>227</b><span>段可复制代码</span></div>
|
|||
|
|
<div class="kpi"><b>21.8 万</b><span>字正文</span></div>
|
|||
|
|
<!-- /AUTO:kpis --></div>
|
|||
|
|
<div class="toolbar">
|
|||
|
|
<div class="search"><input id="q" type="search" placeholder="搜知识点:location / proxy_pass / 限流 / 502 / HTTP/3"></div>
|
|||
|
|
<button class="btn mini" id="expandAll">展开全部小节</button>
|
|||
|
|
<button class="btn mini" id="resetBtn">重置进度</button>
|
|||
|
|
<button class="btn mini" id="printBtn">打印 / 导出 PDF</button>
|
|||
|
|
<div class="progwrap">
|
|||
|
|
<div class="bar"><i id="bar"></i></div>
|
|||
|
|
<b id="pct">0%</b>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<section class="block" id="map">
|
|||
|
|
<h2>知识地图:17 章分别在讲什么</h2>
|
|||
|
|
<p class="sub">六个板块从「它是什么」到「怎么落地」,按顺序读一遍就形成完整心智模型;也可以只挑当前需要的板块。</p>
|
|||
|
|
<div class="map">
|
|||
|
|
<div class="grp">
|
|||
|
|
<h4>① 认知与架构</h4>
|
|||
|
|
<p>nginx 凭什么用几百 MB 内存扛住上万并发;master/worker 怎么分工,一条请求依次经过哪些阶段。</p>
|
|||
|
|
<div class="lk">
|
|||
|
|
<a href="01-what.html">1 全景与选型</a>
|
|||
|
|
<a href="04-arch.html">4 进程模型与阶段</a>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<div class="grp">
|
|||
|
|
<h4>② 安装与配置语言</h4>
|
|||
|
|
<p>装哪一版、有哪些模块;配置怎么分层继承,<code>location</code> 到底谁优先——这是全站的地基。</p>
|
|||
|
|
<div class="lk">
|
|||
|
|
<a href="02-install.html">2 安装与运行控制</a>
|
|||
|
|
<a href="03-config.html">3 配置与 location 匹配</a>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<div class="grp">
|
|||
|
|
<h4>③ 提供服务</h4>
|
|||
|
|
<p>静态资源怎么发得快、缓存头怎么设;反向代理的 URI 与请求头语义;多台上游怎么分流与容错。</p>
|
|||
|
|
<div class="lk">
|
|||
|
|
<a href="05-static.html">5 静态资源服务</a>
|
|||
|
|
<a href="06-proxy.html">6 反向代理</a>
|
|||
|
|
<a href="07-lb.html">7 负载均衡</a>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<div class="grp">
|
|||
|
|
<h4>④ 边界与安全</h4>
|
|||
|
|
<p>HTTPS 与证书治理、变量与重写路由、访问控制、限流限速、敏感信息泄露面。</p>
|
|||
|
|
<div class="lk">
|
|||
|
|
<a href="08-tls.html">8 HTTPS / TLS</a>
|
|||
|
|
<a href="09-rewrite.html">9 变量与重写</a>
|
|||
|
|
<a href="10-security.html">10 访问控制与限流</a>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<div class="grp">
|
|||
|
|
<h4>⑤ 性能与可观测</h4>
|
|||
|
|
<p>该不该在 nginx 做缓存、怎么做微缓存;日志怎么设计才能排障;调优的正确顺序与杠杆。</p>
|
|||
|
|
<div class="lk">
|
|||
|
|
<a href="11-cache.html">11 缓存体系</a>
|
|||
|
|
<a href="12-log.html">12 日志与可观测性</a>
|
|||
|
|
<a href="13-perf.html">13 性能调优</a>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<div class="grp">
|
|||
|
|
<h4>⑥ 落地与查阅</h4>
|
|||
|
|
<p>发布与灰度、502/504/499 排障剧本、本地开发实战、速查表与自测题。</p>
|
|||
|
|
<div class="lk">
|
|||
|
|
<a href="14-ops.html">14 生产运维与排障</a>
|
|||
|
|
<a href="15-devtools.html">15 开发场景实战</a>
|
|||
|
|
<a href="16-cheat.html">16 速查与高频坑</a>
|
|||
|
|
<a href="17-checklist.html">17 自测与面试</a>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<section class="block" id="tracks">
|
|||
|
|
<h2>三条主线:同一套知识,三种用法</h2>
|
|||
|
|
<p class="sub">按你现在的角色挑一条线读,效率最高;三条线合起来就是「开发联调 → 交付发布 → 生产运维」的闭环。</p>
|
|||
|
|
<div class="grid2">
|
|||
|
|
<div class="box note">
|
|||
|
|
<span class="bt">主线 A · 本地开发联调(最常用)</span>
|
|||
|
|
<ol class="steps">
|
|||
|
|
<li>前后端分开跑,用 nginx 把 <code>/api</code> 代理到后端,见 <a href="15-devtools.html">第 15 章</a></li>
|
|||
|
|
<li>把「路径多了一层 / 少了一层」搞明白,见 <a href="06-proxy.html">第 6 章</a></li>
|
|||
|
|
<li>跨域不要只在后端加注解:CORS 预检与 <code>OPTIONS</code> 的处理</li>
|
|||
|
|
<li>本地要 HTTPS 和自定义域名,见 15.4</li>
|
|||
|
|
</ol>
|
|||
|
|
</div>
|
|||
|
|
<div class="box tip">
|
|||
|
|
<span class="bt">主线 B · 交付与 HTTPS</span>
|
|||
|
|
<ol class="steps">
|
|||
|
|
<li>证书怎么签、怎么自动续期、怎么不被扫出过期,见 <a href="08-tls.html">第 8 章</a></li>
|
|||
|
|
<li>发布用 <code>reload</code> 而不是重启,理解它为什么不断连接,见 <a href="04-arch.html">第 4 章</a></li>
|
|||
|
|
<li>灰度 / 蓝绿:按权重与按规则切流,见 <a href="07-lb.html">第 7 章</a></li>
|
|||
|
|
<li>配置进版本库,改动前先 <code>nginx -t</code>,见 <a href="02-install.html">第 2 章</a></li>
|
|||
|
|
</ol>
|
|||
|
|
</div>
|
|||
|
|
<div class="box warn">
|
|||
|
|
<span class="bt">主线 C · 生产运维与排障</span>
|
|||
|
|
<ol class="steps">
|
|||
|
|
<li>502 / 504 / 499 分别是谁的问题,见 <a href="14-ops.html">第 14 章</a></li>
|
|||
|
|
<li>限流为什么「配了没用」——先看 <a href="10-security.html">第 10 章</a> 的实测结论</li>
|
|||
|
|
<li>缓存命中率上不去 / 缓存了不该缓存的东西,见 <a href="11-cache.html">第 11 章</a></li>
|
|||
|
|
<li>日志里要有关键字段才排得动,见 <a href="12-log.html">第 12 章</a></li>
|
|||
|
|
</ol>
|
|||
|
|
</div>
|
|||
|
|
<div class="box ok">
|
|||
|
|
<span class="bt">怎么用这个站点</span>
|
|||
|
|
<ul>
|
|||
|
|
<li><b>左侧目录</b>:列出全部章节,进入某一章后自动展开该章小节并跟随滚动高亮。</li>
|
|||
|
|
<li><b>勾选章节</b>进度保存在本地浏览器(localStorage),刷新不丢。</li>
|
|||
|
|
<li><b>顶栏搜索</b>按关键词过滤下面的章节卡片,也可以直接 <kbd>/</kbd> 聚焦。</li>
|
|||
|
|
<li><b>配置块右上角</b>可以一键复制;支持深色主题与打印 / 导出 PDF。</li>
|
|||
|
|
</ul>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<section class="block" id="chapters-list">
|
|||
|
|
<h2>章节目录</h2>
|
|||
|
|
<p class="sub">共 17 章。每张卡片可以勾选「已读」;展开小节可以直接跳到具体知识点。</p>
|
|||
|
|
<div class="toolbar" style="margin:0 0 16px;padding-top:0;border-top:0">
|
|||
|
|
<div class="search"><input id="q2" type="search" placeholder="也可以在这里过滤章节"></div>
|
|||
|
|
</div>
|
|||
|
|
<!-- AUTO:cards -->
|
|||
|
|
<div class="chapcard" data-search="nginx 全景与选型 nginx 全景与选型:从单机扛住上万并发的真实痛点讲起,说清同步阻塞与事件驱动的每连接成本差异、master/worker/cache 三类进程的分工、nginx 的四类角色、版本谱系与分支发行版,并给出与 apache、caddy、haproxy、envoy、traefik 的取舍依据和典型部署形态。 nginx 选型 事件驱动 c10k worker进程 反向代理 负载均衡 apache caddy haproxy envoy traefik mainline stable openresty tengine 1.1 一个真实痛点:单机扛住上万并发 1.2 同步阻塞 vs 事件驱动:省内存的代价 1.3 master / worker / cache 三类进程 1.4 nginx 的四类角色 1.5 版本谱系:mainline vs stable、开源版 vs plus、openresty / tengine 1.6 与 apache / caddy / haproxy / envoy / traefik 的边界:什么时候不该用 nginx 1.7 典型部署形态">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="1" aria-label="标记第 1 章已读">
|
|||
|
|
<span class="cc-no">1</span>
|
|||
|
|
<a class="cc-title" href="01-what.html">Nginx 全景与选型</a>
|
|||
|
|
<a class="cc-go" href="01-what.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">Nginx 全景与选型:从单机扛住上万并发的真实痛点讲起,说清同步阻塞与事件驱动的每连接成本差异、master/worker/cache 三类进程的分工、nginx 的四类角色、版本谱系与分支发行版,并给出与 Apache、Caddy、HAProxy、Envoy、Traefik 的取舍依据和典型部署形态。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>7 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="01-what.html#sec-1-1">1.1 一个真实痛点:单机扛住上万并发</a><a href="01-what.html#sec-1-2">1.2 同步阻塞 vs 事件驱动:省内存的代价</a><a href="01-what.html#sec-1-3">1.3 master / worker / cache 三类进程</a><a href="01-what.html#sec-1-4">1.4 nginx 的四类角色</a><a href="01-what.html#sec-1-5">1.5 版本谱系:mainline vs stable、开源版 vs Plus、OpenResty / Tengine</a><a href="01-what.html#sec-1-6">1.6 与 Apache / Caddy / HAProxy / Envoy / Traefik 的边界:什么时候不该用 nginx</a><a href="01-what.html#sec-1-7">1.7 典型部署形态</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="安装、目录布局与运行控制 nginx 落地的第一步:四种安装方式的选型依据、源码编译与 configure 参数的取舍、prefix 与运行时目录布局、命令行与六种信号的真实行为与代价、平滑升级与回滚步骤、systemd 托管要点、容器里跑 nginx 的实测注意事项,以及一份可执行的上线自检清单。 nginx安装 源码编译 configure参数 prefix目录布局 nginx信号 reload 平滑升级 systemd 容器 nginx镜像 2.1 四种安装方式与选型 2.2 源码编译与 configure 参数取舍 2.3 目录布局与运行时文件 2.4 命令行与信号 2.5 平滑升级与热二进制替换 2.6 systemd 托管与开机自启 2.7 容器里跑 nginx 的注意事项 2.8 安装后自检清单">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="2" aria-label="标记第 2 章已读">
|
|||
|
|
<span class="cc-no">2</span>
|
|||
|
|
<a class="cc-title" href="02-install.html">安装、目录布局与运行控制</a>
|
|||
|
|
<a class="cc-go" href="02-install.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">Nginx 落地的第一步:四种安装方式的选型依据、源码编译与 configure 参数的取舍、prefix 与运行时目录布局、命令行与六种信号的真实行为与代价、平滑升级与回滚步骤、systemd 托管要点、容器里跑 nginx 的实测注意事项,以及一份可执行的上线自检清单。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>8 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="02-install.html#sec-2-1">2.1 四种安装方式与选型</a><a href="02-install.html#sec-2-2">2.2 源码编译与 configure 参数取舍</a><a href="02-install.html#sec-2-3">2.3 目录布局与运行时文件</a><a href="02-install.html#sec-2-4">2.4 命令行与信号</a><a href="02-install.html#sec-2-5">2.5 平滑升级与热二进制替换</a><a href="02-install.html#sec-2-6">2.6 systemd 托管与开机自启</a><a href="02-install.html#sec-2-7">2.7 容器里跑 nginx 的注意事项</a><a href="02-install.html#sec-2-8">2.8 安装后自检清单</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="配置结构、指令继承与 location 匹配 nginx 配置的地基:分层结构与 include、普通指令与数组型指令的继承差异、location 四种修饰符的真实优先级(附实测)、server_name 匹配顺序与 default_server、变量体系,以及用 nginx -t/-t 调试配置的方法。 nginx.conf location匹配 指令继承 server_name default_server 变量 nginx -t 3.1 配置文件的分层结构与 include 3.2 指令继承:普通指令与数组型指令 3.3 http / server / location 三层各放什么 3.4 location 匹配算法:四种修饰符的真实优先级 3.5 location 的常见误用与正确写法 3.6 server_name 匹配顺序与 default_server 3.7 变量体系:$uri、$request_uri 与惰性求值 3.8 用 nginx -t 与 -t 调试配置 3.9 多站点配置组织:把公共部分抽成 snippets">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="3" aria-label="标记第 3 章已读">
|
|||
|
|
<span class="cc-no">3</span>
|
|||
|
|
<a class="cc-title" href="03-config.html">配置结构、指令继承与 location 匹配</a>
|
|||
|
|
<a class="cc-go" href="03-config.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">Nginx 配置的地基:分层结构与 include、普通指令与数组型指令的继承差异、location 四种修饰符的真实优先级(附实测)、server_name 匹配顺序与 default_server、变量体系,以及用 nginx -t/-T 调试配置的方法。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>9 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="03-config.html#sec-3-1">3.1 配置文件的分层结构与 include</a><a href="03-config.html#sec-3-2">3.2 指令继承:普通指令与数组型指令</a><a href="03-config.html#sec-3-3">3.3 http / server / location 三层各放什么</a><a href="03-config.html#sec-3-4">3.4 location 匹配算法:四种修饰符的真实优先级</a><a href="03-config.html#sec-3-5">3.5 location 的常见误用与正确写法</a><a href="03-config.html#sec-3-6">3.6 server_name 匹配顺序与 default_server</a><a href="03-config.html#sec-3-7">3.7 变量体系:$uri、$request_uri 与惰性求值</a><a href="03-config.html#sec-3-8">3.8 用 nginx -t 与 -T 调试配置</a><a href="03-config.html#sec-3-9">3.9 多站点配置组织:把公共部分抽成 snippets</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="进程模型与请求处理阶段:nginx 为什么快、指令什么时候生效 nginx 为什么快、指令什么时候生效:master/worker/cache 三类进程的职责,epoll 事件驱动为什么不需要每连接一线程,worker_processes/affinity/accept_mutex/worker_connections 的取舍,请求处理的 11 个阶段与模块执行顺序,rewrite 阶段短路掉 limit_req 与 deny 的实测结论,连接与 keepalive 的关系,共享内存区为何必须定义在 http 层,以及 reload 为什么不断连接。 nginx 进程模型 worker 请求处理阶段 epoll keepalive limit_req deny 共享内存 reload 4.1 master 与 worker 的职责(以及 cache manager / cache loader) 4.2 事件驱动与多路复用:为什么不需要每连接一线程 4.3 worker_processes / worker_cpu_affinity / accept_mutex / worker_connections 4.4 请求处理的 11 个阶段与模块执行顺序 4.5 rewrite 阶段 vs preaccess / access:为什么同 location 的 return 会绕过 limit_req 与 deny 4.6 连接、请求与 keepalive 的关系 4.7 共享内存区: limit_req / proxy_cache / ssl session 为什么要定义在 http 层 4.8 reload 为什么能做到不断连接(配合第 2 章的信号)">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="4" aria-label="标记第 4 章已读">
|
|||
|
|
<span class="cc-no">4</span>
|
|||
|
|
<a class="cc-title" href="04-arch.html">进程模型与请求处理阶段:nginx 为什么快、指令什么时候生效</a>
|
|||
|
|
<a class="cc-go" href="04-arch.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">nginx 为什么快、指令什么时候生效:master/worker/cache 三类进程的职责,epoll 事件驱动为什么不需要每连接一线程,worker_processes/affinity/accept_mutex/worker_connections 的取舍,请求处理的 11 个阶段与模块执行顺序,rewrite 阶段短路掉 limit_req 与 deny 的实测结论,连接与 keepalive 的关系,共享内存区为何必须定义在 http 层,以及 reload 为什么不断连接。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>8 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="04-arch.html#sec-4-1">4.1 master 与 worker 的职责(以及 cache manager / cache loader)</a><a href="04-arch.html#sec-4-2">4.2 事件驱动与多路复用:为什么不需要每连接一线程</a><a href="04-arch.html#sec-4-3">4.3 worker_processes / worker_cpu_affinity / accept_mutex / worker_connections</a><a href="04-arch.html#sec-4-4">4.4 请求处理的 11 个阶段与模块执行顺序</a><a href="04-arch.html#sec-4-5">4.5 rewrite 阶段 vs preaccess / access:为什么同 location 的 return 会绕过 limit_req 与 deny</a><a href="04-arch.html#sec-4-6">4.6 连接、请求与 keepalive 的关系</a><a href="04-arch.html#sec-4-7">4.7 共享内存区: limit_req / proxy_cache / ssl session 为什么要定义在 http 层</a><a href="04-arch.html#sec-4-8">4.8 reload 为什么能做到不断连接(配合第 2 章的信号)</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="静态资源服务与缓存头 把 nginx 当静态服务器用到底该怎么写:root 与 alias 的路径算法(附实测)、try_files 与 spa 兜底、索引与 autoindex、mime 与 default_type、sendfile/tcp_nopush/aio 的取舍、gzip 与 brotli 的判断依据,以及 expires/cache-control/etag 这套缓存头的实战配法。 nginx root alias try_files spa 兜底 autoindex mime default_type sendfile gzip_static expires cache-control etag 静态资源缓存 5.1 root 与 alias 的语义差异 5.2 try_files 与 spa 兜底 5.3 索引、目录列表与 autoindex 5.4 mime 类型与 default_type 5.5 sendfile / tcp_nopush / tcp_nodelay / aio 的作用与取舍 5.6 gzip 与 brotli 的取舍、gzip_static 5.7 浏览器缓存:expires、cache-control、etag 与条件请求 5.8 实战:前端产物发布配置(hash 长缓存 + index 不缓存) 5.9 静态服务常见坑速查">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="5" aria-label="标记第 5 章已读">
|
|||
|
|
<span class="cc-no">5</span>
|
|||
|
|
<a class="cc-title" href="05-static.html">静态资源服务与缓存头</a>
|
|||
|
|
<a class="cc-go" href="05-static.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">把 nginx 当静态服务器用到底该怎么写:root 与 alias 的路径算法(附实测)、try_files 与 SPA 兜底、索引与 autoindex、MIME 与 default_type、sendfile/tcp_nopush/aio 的取舍、gzip 与 brotli 的判断依据,以及 expires/Cache-Control/ETag 这套缓存头的实战配法。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>9 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="05-static.html#sec-5-1">5.1 root 与 alias 的语义差异</a><a href="05-static.html#sec-5-2">5.2 try_files 与 SPA 兜底</a><a href="05-static.html#sec-5-3">5.3 索引、目录列表与 autoindex</a><a href="05-static.html#sec-5-4">5.4 MIME 类型与 default_type</a><a href="05-static.html#sec-5-5">5.5 sendfile / tcp_nopush / tcp_nodelay / aio 的作用与取舍</a><a href="05-static.html#sec-5-6">5.6 gzip 与 brotli 的取舍、gzip_static</a><a href="05-static.html#sec-5-7">5.7 浏览器缓存:expires、Cache-Control、ETag 与条件请求</a><a href="05-static.html#sec-5-8">5.8 实战:前端产物发布配置(hash 长缓存 + index 不缓存)</a><a href="05-static.html#sec-5-9">5.9 静态服务常见坑速查</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="反向代理核心:proxy_pass 的完整语义 反向代理的一切坑都在语义上:proxy_pass 带不带 uri 的四种替换结果(实测表格)、四个必配的转发头、回源请求的默认值(http/1.0 与 host=$proxy_host)、上游长连接复用、缓冲与流式取舍、websocket/sse/grpc 三种长连接代理、realip 与后端读取真实 ip、resolver 动态上游,以及一份现象到解决的排错清单。 nginx proxy_pass uri替换 proxy_set_header host x-forwarded-for realip resolver websocket sse grpc upstream keepalive 反向代理排错 6.1 proxy_pass 的 uri 替换规则:一个斜杠决定后端看到什么 6.2 请求头传递:host、xff、x-real-ip、x-forwarded-proto 要一起配 6.3 proxy_set_header 的默认值:默认回源 http/1.0、host 是上游地址 6.4 proxy_http_version 与上游长连接复用 6.5 缓冲与流式:proxy_buffering 与 proxy_request_buffering 6.6 websocket 与 sse 代理 6.7 grpc 代理要点 6.8 realip:nginx 加了头 ≠ 后端拿到真实 ip 6.9 动态上游:resolver、变量 proxy_pass 与容器场景 6.10 反向代理排错清单:现象 → 原因 → 验证 → 解决">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="6" aria-label="标记第 6 章已读">
|
|||
|
|
<span class="cc-no">6</span>
|
|||
|
|
<a class="cc-title" href="06-proxy.html">反向代理核心:proxy_pass 的完整语义</a>
|
|||
|
|
<a class="cc-go" href="06-proxy.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">反向代理的一切坑都在语义上:proxy_pass 带不带 URI 的四种替换结果(实测表格)、四个必配的转发头、回源请求的默认值(HTTP/1.0 与 Host=$proxy_host)、上游长连接复用、缓冲与流式取舍、WebSocket/SSE/gRPC 三种长连接代理、realip 与后端读取真实 IP、resolver 动态上游,以及一份现象到解决的排错清单。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>10 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="06-proxy.html#sec-6-1">6.1 proxy_pass 的 URI 替换规则:一个斜杠决定后端看到什么</a><a href="06-proxy.html#sec-6-2">6.2 请求头传递:Host、XFF、X-Real-IP、X-Forwarded-Proto 要一起配</a><a href="06-proxy.html#sec-6-3">6.3 proxy_set_header 的默认值:默认回源 HTTP/1.0、Host 是上游地址</a><a href="06-proxy.html#sec-6-4">6.4 proxy_http_version 与上游长连接复用</a><a href="06-proxy.html#sec-6-5">6.5 缓冲与流式:proxy_buffering 与 proxy_request_buffering</a><a href="06-proxy.html#sec-6-6">6.6 WebSocket 与 SSE 代理</a><a href="06-proxy.html#sec-6-7">6.7 gRPC 代理要点</a><a href="06-proxy.html#sec-6-8">6.8 realip:nginx 加了头 ≠ 后端拿到真实 IP</a><a href="06-proxy.html#sec-6-9">6.9 动态上游:resolver、变量 proxy_pass 与容器场景</a><a href="06-proxy.html#sec-6-10">6.10 反向代理排错清单:现象 → 原因 → 验证 → 解决</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="负载均衡与上游健康管理 upstream 块与 server 参数逐个讲清(weight/max_conns/max_fails/fail_timeout/backup/down/resolve)、五种调度策略的适用场景与代价、proxy_next_upstream 的默认值为什么只有 error timeout、开源版的主动健康检查替代方案、upstream keepalive 连接池的完整配置,以及灰度切流与负载均衡排障。 nginx upstream 负载均衡 keepalive max_fails proxy_next_upstream split_clients ip_hash 一致性哈希 灰度发布 7.1 upstream 结构与 server 参数:先搞清每个默认值 7.2 五种调度策略:各自的适用场景与代价 7.3 被动健康检查与失败转移:什么情况换后端、什么情况直接报错 7.4 主动健康检查:开源版没有,那怎么办 7.5 upstream keepalive 连接池:最容易配错、收益最直接的一节 7.6 会话保持的三种做法与取舍 7.7 灰度、蓝绿与按比例切流 7.8 负载均衡的典型故障与排障">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="7" aria-label="标记第 7 章已读">
|
|||
|
|
<span class="cc-no">7</span>
|
|||
|
|
<a class="cc-title" href="07-lb.html">负载均衡与上游健康管理</a>
|
|||
|
|
<a class="cc-go" href="07-lb.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">upstream 块与 server 参数逐个讲清(weight/max_conns/max_fails/fail_timeout/backup/down/resolve)、五种调度策略的适用场景与代价、proxy_next_upstream 的默认值为什么只有 error timeout、开源版的主动健康检查替代方案、upstream keepalive 连接池的完整配置,以及灰度切流与负载均衡排障。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>8 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="07-lb.html#sec-7-1">7.1 upstream 结构与 server 参数:先搞清每个默认值</a><a href="07-lb.html#sec-7-2">7.2 五种调度策略:各自的适用场景与代价</a><a href="07-lb.html#sec-7-3">7.3 被动健康检查与失败转移:什么情况换后端、什么情况直接报错</a><a href="07-lb.html#sec-7-4">7.4 主动健康检查:开源版没有,那怎么办</a><a href="07-lb.html#sec-7-5">7.5 upstream keepalive 连接池:最容易配错、收益最直接的一节</a><a href="07-lb.html#sec-7-6">7.6 会话保持的三种做法与取舍</a><a href="07-lb.html#sec-7-7">7.7 灰度、蓝绿与按比例切流</a><a href="07-lb.html#sec-7-8">7.8 负载均衡的典型故障与排障</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="https / tls 与证书治理 nginx 上 https 的完整链条:tls 握手与关键决策、证书链顺序与 sni 多域名、tls 1.2/1.3 套件差异(含 ssl_ciphers 对 1.3 无效的高频误解)、会话复用与前向安全取舍、hsts 与 ocsp stapling、certbot/acme.sh 自动续期与 reload、mtls 及证书信息透传、https 性能杠杆,以及能提前 30/14/7 天报警的证书过期监控方案。 nginx https tls ssl_certificate fullchain 证书链 sni ssl_ciphers tls1.3 ocsp stapling hsts mtls certbot acme.sh 证书过期监控 8.1 tls 握手与 nginx 侧的关键决策 8.2 证书链、sni 与多域名 8.3 协议版本与加密套件 8.4 会话复用:ssl_session_cache / ssl_session_tickets 8.5 hsts、ocsp stapling 与安全响应头 8.6 acme 自动签发与续期 8.7 双向认证 mtls 8.8 https 性能:握手开销、ssl_buffer_size 与 ocsp 的影响 8.9 证书过期事故与监控">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="8" aria-label="标记第 8 章已读">
|
|||
|
|
<span class="cc-no">8</span>
|
|||
|
|
<a class="cc-title" href="08-tls.html">HTTPS / TLS 与证书治理</a>
|
|||
|
|
<a class="cc-go" href="08-tls.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">Nginx 上 HTTPS 的完整链条:TLS 握手与关键决策、证书链顺序与 SNI 多域名、TLS 1.2/1.3 套件差异(含 ssl_ciphers 对 1.3 无效的高频误解)、会话复用与前向安全取舍、HSTS 与 OCSP stapling、certbot/acme.sh 自动续期与 reload、mTLS 及证书信息透传、HTTPS 性能杠杆,以及能提前 30/14/7 天报警的证书过期监控方案。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>9 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="08-tls.html#sec-8-1">8.1 TLS 握手与 nginx 侧的关键决策</a><a href="08-tls.html#sec-8-2">8.2 证书链、SNI 与多域名</a><a href="08-tls.html#sec-8-3">8.3 协议版本与加密套件</a><a href="08-tls.html#sec-8-4">8.4 会话复用:ssl_session_cache / ssl_session_tickets</a><a href="08-tls.html#sec-8-5">8.5 HSTS、OCSP stapling 与安全响应头</a><a href="08-tls.html#sec-8-6">8.6 ACME 自动签发与续期</a><a href="08-tls.html#sec-8-7">8.7 双向认证 mTLS</a><a href="08-tls.html#sec-8-8">8.8 HTTPS 性能:握手开销、ssl_buffer_size 与 OCSP 的影响</a><a href="08-tls.html#sec-8-9">8.9 证书过期事故与监控</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="变量、rewrite 与进阶路由 重定向与改写的进阶规则:return 与 rewrite 的语义差异(含查询串丢失与 absolute_redirect 实测)、rewrite 四个 flag 的行为对比、if 的可靠边界与 map 替代方案、去 www/强制 https/补尾斜杠等常见需求的可抄配置、正则与命名捕获、内部跳转与 named location,以及开放重定向与重定向环的安全边界。 nginx rewrite last break return 301 if is evil map 命名捕获 error_page named location 开放重定向 absolute_redirect 9.1 return 与 rewrite 的语义差异 9.2 rewrite 的四个 flag:last / break / redirect / permanent 9.3 if 的真相:为什么说 if is evil,什么情况下可以安全使用 9.4 map:把条件逻辑从 if 里搬出来 9.5 常见重定向需求:可直接抄的四组配置 9.6 正则捕获与命名捕获 9.7 内部跳转、error_page 与 named location 9.8 路由改造的安全边界:开放重定向、重定向环与 location 注入">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="9" aria-label="标记第 9 章已读">
|
|||
|
|
<span class="cc-no">9</span>
|
|||
|
|
<a class="cc-title" href="09-rewrite.html">变量、rewrite 与进阶路由</a>
|
|||
|
|
<a class="cc-go" href="09-rewrite.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">重定向与改写的进阶规则:return 与 rewrite 的语义差异(含查询串丢失与 absolute_redirect 实测)、rewrite 四个 flag 的行为对比、if 的可靠边界与 map 替代方案、去 www/强制 https/补尾斜杠等常见需求的可抄配置、正则与命名捕获、内部跳转与 named location,以及开放重定向与重定向环的安全边界。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>8 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="09-rewrite.html#sec-9-1">9.1 return 与 rewrite 的语义差异</a><a href="09-rewrite.html#sec-9-2">9.2 rewrite 的四个 flag:last / break / redirect / permanent</a><a href="09-rewrite.html#sec-9-3">9.3 if 的真相:为什么说 if is evil,什么情况下可以安全使用</a><a href="09-rewrite.html#sec-9-4">9.4 map:把条件逻辑从 if 里搬出来</a><a href="09-rewrite.html#sec-9-5">9.5 常见重定向需求:可直接抄的四组配置</a><a href="09-rewrite.html#sec-9-6">9.6 正则捕获与命名捕获</a><a href="09-rewrite.html#sec-9-7">9.7 内部跳转、error_page 与 named location</a><a href="09-rewrite.html#sec-9-8">9.8 路由改造的安全边界:开放重定向、重定向环与 Location 注入</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="访问控制、限流限速与安全加固 把 nginx 当成边界上的第一道防线:威胁模型与三类流量划分,allow/deny/geo/map 的 ip 策略(含 deny 被同 location 的 return 短路的实测),auth_basic 与 auth_request 子请求鉴权,limit_req 的 rate/burst/nodelay/delay 漏桶语义与多级限流,limit_conn 与 limit_rate,请求体与请求头限制的参数默认值表,防盗链与 ua 治理,敏感文件与信息泄露防护,安全响应头与 csp 取舍,以及一份可勾可验证的上线前检查清单。 nginx 访问控制 allow deny geo map auth_basic auth_request limit_req burst nodelay 漏桶 limit_conn limit_rate client_max_body_size 413 server_tokens 安全响应头 csp hsts 上线检查清单 10.1 威胁模型:把流量分成「该来的」「不该来的」「来太多的」 10.2 allow / deny / geo / map 做 ip 策略 10.3 认证:auth_basic 与 auth_request 子请求鉴权 10.4 限流: limit_req 的 rate / burst / nodelay / delay 10.5 限连与限速: limit_conn 、 limit_rate 、 limit_rate_after 10.6 请求体与请求头限制:默认值 1m 背后的取舍 10.7 防盗链、防爬与 ua 治理 10.8 敏感文件与信息泄露:从 server_tokens 到 .git 10.9 安全响应头与 csp: add_header 的两个陷阱 10.10 上线前安全检查清单">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="10" aria-label="标记第 10 章已读">
|
|||
|
|
<span class="cc-no">10</span>
|
|||
|
|
<a class="cc-title" href="10-security.html">访问控制、限流限速与安全加固</a>
|
|||
|
|
<a class="cc-go" href="10-security.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">把 nginx 当成边界上的第一道防线:威胁模型与三类流量划分,allow/deny/geo/map 的 IP 策略(含 deny 被同 location 的 return 短路的实测),auth_basic 与 auth_request 子请求鉴权,limit_req 的 rate/burst/nodelay/delay 漏桶语义与多级限流,limit_conn 与 limit_rate,请求体与请求头限制的参数默认值表,防盗链与 UA 治理,敏感文件与信息泄露防护,安全响应头与 CSP 取舍,以及一份可勾可验证的上线前检查清单。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>10 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="10-security.html#sec-10-1">10.1 威胁模型:把流量分成「该来的」「不该来的」「来太多的」</a><a href="10-security.html#sec-10-2">10.2 allow / deny / geo / map 做 IP 策略</a><a href="10-security.html#sec-10-3">10.3 认证:auth_basic 与 auth_request 子请求鉴权</a><a href="10-security.html#sec-10-4">10.4 限流: limit_req 的 rate / burst / nodelay / delay</a><a href="10-security.html#sec-10-5">10.5 限连与限速: limit_conn 、 limit_rate 、 limit_rate_after</a><a href="10-security.html#sec-10-6">10.6 请求体与请求头限制:默认值 1m 背后的取舍</a><a href="10-security.html#sec-10-7">10.7 防盗链、防爬与 UA 治理</a><a href="10-security.html#sec-10-8">10.8 敏感文件与信息泄露:从 server_tokens 到 .git</a><a href="10-security.html#sec-10-9">10.9 安全响应头与 CSP: add_header 的两个陷阱</a><a href="10-security.html#sec-10-10">10.10 上线前安全检查清单</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="缓存体系:proxy_cache 与微缓存 nginx 缓存的完整语义:浏览器/cdn/nginx 三层缓存的边界,proxy_cache_path 的 keys_zone 与 max_size 到底各管什么,默认 cache key 为什么会串号,proxy_cache_valid 与 use_stale 的降级写法,开源版怎么清理缓存,微缓存如何把 1000 qps 的回源压到 1 次每秒,以及用 $upstream_cache_status 统计命中率与排查失效原因。 nginx proxy_cache keys_zone proxy_cache_key microcaching 微缓存 stale-if-error 命中率 upstream_cache_status 11.1 该不该在 nginx 做缓存:浏览器 / cdn / nginx 三层各管什么 11.2 proxy_cache_path 的关键参数与磁盘布局 11.3 cache key 设计:proxy_cache_key 与 vary 11.4 有效期与降级:proxy_cache_valid、use_stale 与 stale-if-error 11.5 缓存绕过与清理:proxy_cache_bypass、proxy_no_cache 与清理的代价 11.6 微缓存(microcaching)抗突发流量 11.7 fastcgi / uwsgi / scgi 缓存与静态文件缓存(open_file_cache) 11.8 命中率观测与常见失效问题">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="11" aria-label="标记第 11 章已读">
|
|||
|
|
<span class="cc-no">11</span>
|
|||
|
|
<a class="cc-title" href="11-cache.html">缓存体系:proxy_cache 与微缓存</a>
|
|||
|
|
<a class="cc-go" href="11-cache.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">nginx 缓存的完整语义:浏览器/CDN/nginx 三层缓存的边界,proxy_cache_path 的 keys_zone 与 max_size 到底各管什么,默认 cache key 为什么会串号,proxy_cache_valid 与 use_stale 的降级写法,开源版怎么清理缓存,微缓存如何把 1000 QPS 的回源压到 1 次每秒,以及用 $upstream_cache_status 统计命中率与排查失效原因。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>8 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="11-cache.html#sec-11-1">11.1 该不该在 nginx 做缓存:浏览器 / CDN / nginx 三层各管什么</a><a href="11-cache.html#sec-11-2">11.2 proxy_cache_path 的关键参数与磁盘布局</a><a href="11-cache.html#sec-11-3">11.3 cache key 设计:proxy_cache_key 与 Vary</a><a href="11-cache.html#sec-11-4">11.4 有效期与降级:proxy_cache_valid、use_stale 与 stale-if-error</a><a href="11-cache.html#sec-11-5">11.5 缓存绕过与清理:proxy_cache_bypass、proxy_no_cache 与清理的代价</a><a href="11-cache.html#sec-11-6">11.6 微缓存(microcaching)抗突发流量</a><a href="11-cache.html#sec-11-7">11.7 fastcgi / uwsgi / scgi 缓存与静态文件缓存(open_file_cache)</a><a href="11-cache.html#sec-11-8">11.8 命中率观测与常见失效问题</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="日志体系与可观测性:让 502 自己说出原因 把 nginx 的日志从「事后翻文件」变成可告警的观测能力:access_log 与 error_log 的职责边界与级别取舍、一份生产可用的 log_format 与逐字段说明、escape=json 结构化日志的写法与空值陷阱、上游耗时变量的归因表、logrotate 与 usr1 的切割闭环、缓冲与条件日志的代价、接 elk/loki/clickhouse 的落地方案、stub_status 的字段与 prometheus 告警规则,以及一份日志排障信号清单。 nginx access_log error_log log_format json日志 escape=json upstream_response_time 日志切割 usr1 stub_status prometheus 12.1 access_log 与 error_log 的职责边界 12.2 log_format 与变量:设计一份可分析的日志 12.3 json 结构化日志:escape=json 与空值陷阱 12.4 上游耗时变量与性能归因 12.5 日志切割:logrotate 与 usr1 信号 12.6 日志缓冲、条件日志与性能代价 12.7 接 elk / loki / clickhouse 的落地方案 12.8 stub_status 与 prometheus 指标 12.9 日志里的排障信号清单">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="12" aria-label="标记第 12 章已读">
|
|||
|
|
<span class="cc-no">12</span>
|
|||
|
|
<a class="cc-title" href="12-log.html">日志体系与可观测性:让 502 自己说出原因</a>
|
|||
|
|
<a class="cc-go" href="12-log.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">把 nginx 的日志从「事后翻文件」变成可告警的观测能力:access_log 与 error_log 的职责边界与级别取舍、一份生产可用的 log_format 与逐字段说明、escape=json 结构化日志的写法与空值陷阱、上游耗时变量的归因表、logrotate 与 USR1 的切割闭环、缓冲与条件日志的代价、接 ELK/Loki/ClickHouse 的落地方案、stub_status 的字段与 Prometheus 告警规则,以及一份日志排障信号清单。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>9 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="12-log.html#sec-12-1">12.1 access_log 与 error_log 的职责边界</a><a href="12-log.html#sec-12-2">12.2 log_format 与变量:设计一份可分析的日志</a><a href="12-log.html#sec-12-3">12.3 JSON 结构化日志:escape=json 与空值陷阱</a><a href="12-log.html#sec-12-4">12.4 上游耗时变量与性能归因</a><a href="12-log.html#sec-12-5">12.5 日志切割:logrotate 与 USR1 信号</a><a href="12-log.html#sec-12-6">12.6 日志缓冲、条件日志与性能代价</a><a href="12-log.html#sec-12-7">12.7 接 ELK / Loki / ClickHouse 的落地方案</a><a href="12-log.html#sec-12-8">12.8 stub_status 与 Prometheus 指标</a><a href="12-log.html#sec-12-9">12.9 日志里的排障信号清单</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="性能调优与协议演进:先测量,再改一个变量 nginx 性能调优的完整方法:先建立基线并用 $request_time / $upstream_response_time 分离瓶颈,再算 worker_processes 与 worker_connections 的连接预算、把 worker_rlimit_nofile 与 systemd limitnofile、ulimit、fs.file-max 一起调;逐条讲清缓冲区与超时参数的默认值和内存代价、内核参数该动哪些不该动哪些、http/1.1 keepalive 与 http/2 / http/3 的取舍依据、静态资源组合拳、wrk/ab/k6 的压测方法与指标解读、常见性能反模式,以及用利特尔法则做容量估算与压测清单。 nginx 性能调优 worker_connections worker_rlimit_nofile proxy_buffers 超时 内核参数 http/2 http/3 wrk 压测 容量估算 p99 13.1 调优方法论:没有基线的调优都是猜 13.2 worker 与连接数配置的算法:把上限算出来,而不是抄过来 13.3 缓冲区与超时参数:每一个都有默认值,每一个都有代价 13.4 内核参数与文件描述符:哪些能动,哪些碰都不要碰 13.5 http/1.1 keepalive、http/2、http/3:不是版本越高越好 13.6 静态资源性能组合拳:让请求不要来,比让请求更快更重要 13.7 压测方法与指标解读:qps 是结果,不是目标 13.8 常见性能反模式:每一条都有具体后果 13.9 容量估算与压测清单:把「能扛多少」算出来">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="13" aria-label="标记第 13 章已读">
|
|||
|
|
<span class="cc-no">13</span>
|
|||
|
|
<a class="cc-title" href="13-perf.html">性能调优与协议演进:先测量,再改一个变量</a>
|
|||
|
|
<a class="cc-go" href="13-perf.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">nginx 性能调优的完整方法:先建立基线并用 $request_time / $upstream_response_time 分离瓶颈,再算 worker_processes 与 worker_connections 的连接预算、把 worker_rlimit_nofile 与 systemd LimitNOFILE、ulimit、fs.file-max 一起调;逐条讲清缓冲区与超时参数的默认值和内存代价、内核参数该动哪些不该动哪些、HTTP/1.1 keepalive 与 HTTP/2 / HTTP/3 的取舍依据、静态资源组合拳、wrk/ab/k6 的压测方法与指标解读、常见性能反模式,以及用利特尔法则做容量估算与压测清单。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>9 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="13-perf.html#sec-13-1">13.1 调优方法论:没有基线的调优都是猜</a><a href="13-perf.html#sec-13-2">13.2 worker 与连接数配置的算法:把上限算出来,而不是抄过来</a><a href="13-perf.html#sec-13-3">13.3 缓冲区与超时参数:每一个都有默认值,每一个都有代价</a><a href="13-perf.html#sec-13-4">13.4 内核参数与文件描述符:哪些能动,哪些碰都不要碰</a><a href="13-perf.html#sec-13-5">13.5 HTTP/1.1 keepalive、HTTP/2、HTTP/3:不是版本越高越好</a><a href="13-perf.html#sec-13-6">13.6 静态资源性能组合拳:让请求不要来,比让请求更快更重要</a><a href="13-perf.html#sec-13-7">13.7 压测方法与指标解读:QPS 是结果,不是目标</a><a href="13-perf.html#sec-13-8">13.8 常见性能反模式:每一条都有具体后果</a><a href="13-perf.html#sec-13-9">13.9 容量估算与压测清单:把「能扛多少」算出来</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="生产运维、发布与排障手册 把 nginx 当生产系统来运维:配置进版本库与可回滚的变更流程、nginx -s reload 的失败语义与灰度上线手法、keepalived/vip 与云 lb 的取舍和脑裂风险、状态码全景归因、502/504/499 分支排障剧本、连接数与文件描述符耗尽、日志与 proxy_cache 目录治理、告警指标与阈值思路,以及可填写的事故复盘模板与 on-call 清单。 nginx 生产运维 reload 灰度 502 504 499 keepalived 脑裂 文件描述符 proxy_cache 监控告警 复盘 on-call 14.1 配置管理与变更流程:把 nginx.conf 当代码 14.2 平滑 reload 与灰度上线 14.3 高可用:keepalived / vip 与云 lb 的取舍、脑裂风险 14.4 状态码全景与归因:先确认「这个码是谁产生的」 14.5 502 / 504 / 499 排障剧本(本章核心) 14.6 连接数与文件描述符耗尽:四个上限之间的关系 14.7 磁盘与缓存目录治理:日志涨满、proxy_cache 涨满 14.8 监控告警:5–8 条真正值得响的指标与阈值思路 14.9 事故复盘模板与 on-call 清单">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="14" aria-label="标记第 14 章已读">
|
|||
|
|
<span class="cc-no">14</span>
|
|||
|
|
<a class="cc-title" href="14-ops.html">生产运维、发布与排障手册</a>
|
|||
|
|
<a class="cc-go" href="14-ops.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">把 nginx 当生产系统来运维:配置进版本库与可回滚的变更流程、nginx -s reload 的失败语义与灰度上线手法、Keepalived/VIP 与云 LB 的取舍和脑裂风险、状态码全景归因、502/504/499 分支排障剧本、连接数与文件描述符耗尽、日志与 proxy_cache 目录治理、告警指标与阈值思路,以及可填写的事故复盘模板与 on-call 清单。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>9 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="14-ops.html#sec-14-1">14.1 配置管理与变更流程:把 nginx.conf 当代码</a><a href="14-ops.html#sec-14-2">14.2 平滑 reload 与灰度上线</a><a href="14-ops.html#sec-14-3">14.3 高可用:Keepalived / VIP 与云 LB 的取舍、脑裂风险</a><a href="14-ops.html#sec-14-4">14.4 状态码全景与归因:先确认「这个码是谁产生的」</a><a href="14-ops.html#sec-14-5">14.5 502 / 504 / 499 排障剧本(本章核心)</a><a href="14-ops.html#sec-14-6">14.6 连接数与文件描述符耗尽:四个上限之间的关系</a><a href="14-ops.html#sec-14-7">14.7 磁盘与缓存目录治理:日志涨满、proxy_cache 涨满</a><a href="14-ops.html#sec-14-8">14.8 监控告警:5–8 条真正值得响的指标与阈值思路</a><a href="14-ops.html#sec-14-9">14.9 事故复盘模板与 on-call 清单</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="开发场景实战:把 nginx 用进日常 本地开发到底什么时候需要 nginx(和 vite/webpack devserver proxy 的边界与判据)、前后端联调的三种拓扑与 host.docker.internal 的坑、cors 预检为什么最容易漏、mkcert 本地 https 与自定义域名、mock 接口分流、多分支并行、docker compose 服务名上游与动态解析、ingress-nginx 注解对照,以及一份可直接保存使用的开发配置模板。 nginx 本地开发 联调代理 devserver proxy cors 预检 mkcert 本地https hosts mock分流 多分支 docker compose ingress-nginx 注解 开发配置模板 15.1 本地开发为什么需要 nginx:和 devserver proxy 的边界 15.2 前后端联调代理:本机、容器、测试环境三种情形 15.3 cors 的正确配法:先把预检请求活着放过去 15.4 本地 https 与自定义域名:mkcert、hosts 与 dnsmasq 15.5 mock 与接口分流:只把一部分接口指向假数据 15.6 多环境与多分支并行:用域名隔离,不要用端口隔离 15.7 docker compose 里的 nginx:服务名当上游、动态解析、热加载 15.8 kubernetes ingress 与 nginx 的关系:注解就是生成的配置 15.9 一份可直接用的开发配置模板">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="15" aria-label="标记第 15 章已读">
|
|||
|
|
<span class="cc-no">15</span>
|
|||
|
|
<a class="cc-title" href="15-devtools.html">开发场景实战:把 Nginx 用进日常</a>
|
|||
|
|
<a class="cc-go" href="15-devtools.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">本地开发到底什么时候需要 nginx(和 Vite/webpack devServer proxy 的边界与判据)、前后端联调的三种拓扑与 host.docker.internal 的坑、CORS 预检为什么最容易漏、mkcert 本地 HTTPS 与自定义域名、Mock 接口分流、多分支并行、Docker Compose 服务名上游与动态解析、ingress-nginx 注解对照,以及一份可直接保存使用的开发配置模板。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>9 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="15-devtools.html#sec-15-1">15.1 本地开发为什么需要 nginx:和 devServer proxy 的边界</a><a href="15-devtools.html#sec-15-2">15.2 前后端联调代理:本机、容器、测试环境三种情形</a><a href="15-devtools.html#sec-15-3">15.3 CORS 的正确配法:先把预检请求活着放过去</a><a href="15-devtools.html#sec-15-4">15.4 本地 HTTPS 与自定义域名:mkcert、hosts 与 dnsmasq</a><a href="15-devtools.html#sec-15-5">15.5 Mock 与接口分流:只把一部分接口指向假数据</a><a href="15-devtools.html#sec-15-6">15.6 多环境与多分支并行:用域名隔离,不要用端口隔离</a><a href="15-devtools.html#sec-15-7">15.7 Docker Compose 里的 nginx:服务名当上游、动态解析、热加载</a><a href="15-devtools.html#sec-15-8">15.8 Kubernetes Ingress 与 nginx 的关系:注解就是生成的配置</a><a href="15-devtools.html#sec-15-9">15.9 一份可直接用的开发配置模板</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="速查表与高频坑 nginx 速查手册:命令行与信号、可直接复制的配置片段、常用变量表、状态码归因、20 个高频坑(现象→原因→解决)、一条命令排障表,以及带实测证据的默认值速查。 nginx 速查 状态码 502 504 499 变量 默认值 高频坑 reload 16.1 命令与信号速查 16.2 配置片段速查 16.3 常用变量速查 16.4 状态码速查与归因 16.5 20 个高频坑 16.6 一条命令排障表 16.7 默认值速查(附实测) 16.8 接下来怎么走">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="16" aria-label="标记第 16 章已读">
|
|||
|
|
<span class="cc-no">16</span>
|
|||
|
|
<a class="cc-title" href="16-cheat.html">速查表与高频坑</a>
|
|||
|
|
<a class="cc-go" href="16-cheat.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">Nginx 速查手册:命令行与信号、可直接复制的配置片段、常用变量表、状态码归因、20 个高频坑(现象→原因→解决)、一条命令排障表,以及带实测证据的默认值速查。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>8 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="16-cheat.html#sec-16-1">16.1 命令与信号速查</a><a href="16-cheat.html#sec-16-2">16.2 配置片段速查</a><a href="16-cheat.html#sec-16-3">16.3 常用变量速查</a><a href="16-cheat.html#sec-16-4">16.4 状态码速查与归因</a><a href="16-cheat.html#sec-16-5">16.5 20 个高频坑</a><a href="16-cheat.html#sec-16-6">16.6 一条命令排障表</a><a href="16-cheat.html#sec-16-7">16.7 默认值速查(附实测)</a><a href="16-cheat.html#sec-16-8">16.8 接下来怎么走</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<div class="chapcard" data-search="能力自测、面试问答与继续深入 nginx 能力自测:入门/进阶/生产三档清单、40 道自测题(附答案要点与回看章节)、面试高频问答与追问方向、3 天/2 周/1 月学习路径,以及官方文档地图与延伸阅读。 nginx 面试题 自测 学习路径 location proxy_pass 负载均衡 排障 17.1 能力自测清单(入门 / 进阶 / 生产) 17.2 40 道自测题(附答案要点) 17.3 面试高频问答与追问方向 17.4 学习路径:3 天 / 2 周 / 1 月 17.5 官方文档地图与延伸阅读">
|
|||
|
|
<div class="cc-head">
|
|||
|
|
<input type="checkbox" data-ch="17" aria-label="标记第 17 章已读">
|
|||
|
|
<span class="cc-no">17</span>
|
|||
|
|
<a class="cc-title" href="17-checklist.html">能力自测、面试问答与继续深入</a>
|
|||
|
|
<a class="cc-go" href="17-checklist.html">进入 →</a>
|
|||
|
|
</div>
|
|||
|
|
<p class="cc-desc">Nginx 能力自测:入门/进阶/生产三档清单、40 道自测题(附答案要点与回看章节)、面试高频问答与追问方向、3 天/2 周/1 月学习路径,以及官方文档地图与延伸阅读。</p>
|
|||
|
|
<details class="cc-secs">
|
|||
|
|
<summary>5 个小节 · 展开看知识点</summary>
|
|||
|
|
<div><a href="17-checklist.html#sec-17-1">17.1 能力自测清单(入门 / 进阶 / 生产)</a><a href="17-checklist.html#sec-17-2">17.2 40 道自测题(附答案要点)</a><a href="17-checklist.html#sec-17-3">17.3 面试高频问答与追问方向</a><a href="17-checklist.html#sec-17-4">17.4 学习路径:3 天 / 2 周 / 1 月</a><a href="17-checklist.html#sec-17-5">17.5 官方文档地图与延伸阅读</a></div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
<!-- /AUTO:cards -->
|
|||
|
|
<div class="empty" id="noHit" style="display:none">没有匹配的章节,换个关键词试试(比如 <code>location</code>、<code>代理</code>、<code>限流</code>、<code>502</code>)。</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<section class="block" id="start">
|
|||
|
|
<h2>5 分钟上手:从零到跑起一个「静态 + 反向代理」入口</h2>
|
|||
|
|
<p class="sub">已经装好 nginx 的话从这里开始;安装细节与目录布局见 <a href="02-install.html">第 2 章</a>,配置语义见 <a href="03-config.html">第 3 章</a>。</p>
|
|||
|
|
<ol class="steps">
|
|||
|
|
<li>先确认版本与编译进去的模块(<mark>很多「指令不认识」其实是模块没编进去</mark>):
|
|||
|
|
<pre data-lang="bash" data-name="自检">$ nginx -v # 版本,例如 nginx/1.27.5
|
|||
|
|
$ nginx -V 2>&1 # 编译参数:看 --with-http_ssl_module / --with-http_v2_module 等
|
|||
|
|
$ nginx -t # 语法自检,改配置后必须先跑这个
|
|||
|
|
$ nginx -T # 打印「生效的完整配置」,排查继承问题最有用</pre>
|
|||
|
|
</li>
|
|||
|
|
<li>写一份最小可用的入口配置:静态资源交给 nginx,<code>/api</code> 转给后端:
|
|||
|
|
<pre data-lang="nginx" data-name="/etc/nginx/conf.d/app.conf">server {
|
|||
|
|
listen 80;
|
|||
|
|
server_name app.local;
|
|||
|
|
|
|||
|
|
root /var/www/app;
|
|||
|
|
index index.html;
|
|||
|
|
|
|||
|
|
# 前端单页应用:找不到文件就回 index.html
|
|||
|
|
location / {
|
|||
|
|
try_files $uri $uri/ /index.html;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 反向代理:注意 proxy_pass 结尾的 / 会替换掉 /api 前缀
|
|||
|
|
location /api/ {
|
|||
|
|
proxy_pass http://127.0.0.1:8080/;
|
|||
|
|
proxy_http_version 1.1;
|
|||
|
|
proxy_set_header Host $host;
|
|||
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|||
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|||
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|||
|
|
}
|
|||
|
|
}</pre>
|
|||
|
|
</li>
|
|||
|
|
<li>让配置生效。开发机上 <code>reload</code> 就够,<b>不会断开已有连接</b>:
|
|||
|
|
<pre data-lang="bash" data-name="生效与验证">$ nginx -t && nginx -s reload
|
|||
|
|
$ curl -sI http://app.local/ # 看 200 与响应头
|
|||
|
|
$ curl -s http://app.local/api/ping # 看后端是否收到 /ping(而不是 /api/ping)
|
|||
|
|
$ tail -f /var/log/nginx/error.log # 出问题先看这里,再看 access.log</pre>
|
|||
|
|
</li>
|
|||
|
|
<li>线上还要补的三件事:<b>HTTPS</b>(<a href="08-tls.html">第 8 章</a>)、
|
|||
|
|
<b>限流与访问控制</b>(<a href="10-security.html">第 10 章</a>)、
|
|||
|
|
<b>日志字段设计</b>(<a href="12-log.html">第 12 章</a>)。
|
|||
|
|
只改配置不改这些,出了事故会很难定位。</li>
|
|||
|
|
</ol>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<section class="block" id="pitfalls">
|
|||
|
|
<h2>开始之前:8 个最常见的坑</h2>
|
|||
|
|
<p class="sub">这些坑几乎每个配过 nginx 的人都踩过,先看一眼能省很多时间;完整 20 条见 <a href="16-cheat.html">第 16 章</a>。</p>
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>现象</th><th>最常见的原因</th><th>去哪一章看</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>代理之后后端 404,路径莫名多/少一层</td><td><code>proxy_pass</code> 结尾带不带 URI(哪怕只是一个 <code>/</code>)会决定 <b>location 前缀是否被替换</b></td><td><a href="06-proxy.html">6.1</a></td></tr>
|
|||
|
|
<tr><td>后端拿到的客户端 IP 全是 nginx 自己的</td><td>默认<b>不发送</b> <code>X-Forwarded-For</code> / <code>X-Real-IP</code>,必须显式 <code>proxy_set_header</code>;后端还得配 realip 才读得到</td><td><a href="06-proxy.html">6.2</a></td></tr>
|
|||
|
|
<tr><td>后端虚拟主机路由错、回调域名不对</td><td>默认 <code>Host</code> 是 <code>$proxy_host</code>(上游地址),<b>不是</b>原始域名</td><td><a href="06-proxy.html">6.3</a></td></tr>
|
|||
|
|
<tr><td>在 location 里加了一个 <code>add_header</code>,别的头全没了</td><td><code>add_header</code> <b>不做合并式继承</b>:子级写了任意一个,父级全部失效</td><td><a href="05-static.html">5.7</a></td></tr>
|
|||
|
|
<tr><td>404 / 502 响应上没有安全头</td><td><code>add_header</code> 默认只对 2xx/3xx 生效,错误响应要加 <code>always</code></td><td><a href="10-security.html">10.9</a></td></tr>
|
|||
|
|
<tr><td>重定向跳到了 <code>http://127.0.0.1/...</code> 还丢了参数</td><td><code>return 301</code> 会丢掉原始查询串;且默认 <code>absolute_redirect on</code>,用的端口是 nginx 自己的 <code>listen</code> 端口</td><td><a href="09-rewrite.html">9.1</a></td></tr>
|
|||
|
|
<tr><td>限流「配了但没生效」,一点没拦住</td><td>同一个 location 里写了 <code>return</code>:它在 rewrite 阶段就终结了请求,<code>limit_req</code> 所在的 preaccess 阶段根本没轮到(<code>deny</code> 同理被绕过)</td><td><a href="10-security.html">10.4</a></td></tr>
|
|||
|
|
<tr><td>改完配置 reload 了,行为没变化</td><td>改的不是生效的文件(<code>include</code> 没覆盖到)、或 reload 前没 <code>nginx -t</code> 导致旧配置继续跑</td><td><a href="02-install.html">2.4</a></td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<section class="block" id="verified">
|
|||
|
|
<h2>本站的结论是怎么来的:38 条实测</h2>
|
|||
|
|
<p class="sub">Nginx 的很多「常识」在网上互相矛盾。本站凡是反直觉的结论,都用真实容器跑过。</p>
|
|||
|
|
<div class="grid2">
|
|||
|
|
<div class="box case">
|
|||
|
|
<span class="bt">可复现的核验脚本</span>
|
|||
|
|
<p>仓库里带了 <code>tools/verify-behavior.js</code>:它起一个
|
|||
|
|
<code>nginx:1.27-alpine</code> 容器,注入一份专门覆盖这些行为的配置,
|
|||
|
|
然后逐条比对状态码 / 响应头 / 响应体,并把 <code>error.log</code> 里的关键行
|
|||
|
|
(谁真的被限流、谁真的被拒绝)一起打出来。</p>
|
|||
|
|
<pre data-lang="bash" data-name="自己跑一遍">$ cd knowledge/nginx
|
|||
|
|
$ node tools/verify-behavior.js # 需要本机有可用的 Docker
|
|||
|
|
# 38 条结论全部与实测一致才会退出 0</pre>
|
|||
|
|
</div>
|
|||
|
|
<div class="box ok">
|
|||
|
|
<span class="bt">实测核验的版本</span>
|
|||
|
|
<ul>
|
|||
|
|
<li>镜像:官方 <code>nginx:1.27-alpine</code></li>
|
|||
|
|
<li>实际版本:<b>nginx/1.27.5</b></li>
|
|||
|
|
<li>覆盖:location 优先级、root/alias、try_files、<code>proxy_pass</code> 的 URI 与默认回源头、
|
|||
|
|
<code>add_header</code> 继承、<code>return</code> 的参数与短路行为、<code>$uri</code> 归一化、
|
|||
|
|
<code>limit_req</code> 的 burst 语义、以及一批默认值</li>
|
|||
|
|
<li>每条结论的原文与证据见 <a href="16-cheat.html">16.7 默认值速查</a> 与
|
|||
|
|
<code>tools/SPEC.md</code> 第 6 节</li>
|
|||
|
|
</ul>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<section class="block" id="related">
|
|||
|
|
<h2>和其它资料的关系</h2>
|
|||
|
|
<div class="grid2">
|
|||
|
|
<div class="box">
|
|||
|
|
<h4>Nginx 专题 ↔ Docker / Linux 专题</h4>
|
|||
|
|
<ul>
|
|||
|
|
<li>本专题是<b>纵深</b>:把 nginx 这一件事讲到能配、能调、能排障。</li>
|
|||
|
|
<li><a href="../docker/index.html">Docker 专题</a> 解决「服务怎么打包、怎么起」,
|
|||
|
|
nginx 经常就是那个被起的容器;容器里跑 nginx 的注意点见 <a href="02-install.html">2.7</a>。</li>
|
|||
|
|
<li><a href="../linux/index.html">Linux 专题</a> 是地基:文件描述符、内核参数、
|
|||
|
|
<code>ss</code>/<code>tcpdump</code>/<code>strace</code> 这些排障手段,在
|
|||
|
|
<a href="14-ops.html">第 14 章</a> 会直接用到。</li>
|
|||
|
|
</ul>
|
|||
|
|
</div>
|
|||
|
|
<div class="box">
|
|||
|
|
<h4>建议的阅读顺序</h4>
|
|||
|
|
<ul>
|
|||
|
|
<li><b>完全新手</b>:1 → 3 → 5 → 6 → 15,先能配出一个可用入口。</li>
|
|||
|
|
<li><b>要上生产</b>:再加 7 → 8 → 10 → 11 → 12 → 14。</li>
|
|||
|
|
<li><b>面试 / 复习</b>:16 速查 + 17 自测,不会的跳回对应章。</li>
|
|||
|
|
<li><b>正在排障</b>:先去 <a href="14-ops.html">14.5</a> 的 502/504/499 剧本,
|
|||
|
|
再看 <a href="12-log.html">第 12 章</a> 确认日志字段够不够用。</li>
|
|||
|
|
</ul>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
</article>
|
|||
|
|
|
|||
|
|
<nav class="pager">
|
|||
|
|
<a class="next" href="01-what.html"><span class="k">开始阅读 →</span><span class="v">第 1 章 · Nginx 全景与选型</span></a>
|
|||
|
|
</nav>
|
|||
|
|
|
|||
|
|
<p class="foot">
|
|||
|
|
Nginx 专题知识库 · 共 17 章 143 个小节 · 227 段代码<br>
|
|||
|
|
<a href="index.html">总目录</a> · <a href="../docker/index.html">Docker 专题</a> · <a href="../linux/index.html">Linux 专题</a>
|
|||
|
|
</p>
|
|||
|
|
</main>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<button id="toTop" title="回到顶部">↑</button>
|
|||
|
|
<script src="assets/doc.js"></script>
|
|||
|
|
<script src="../../assets/back.js" data-portal-back="knowledge/nginx" data-portal-label="知识库 › Nginx 专题知识库"></script>
|
|||
|
|
<script>
|
|||
|
|
/* ==========================================================================
|
|||
|
|
Nginx 专题 · 目录页交互
|
|||
|
|
章节卡片由 tools/build-nav.js 依据各章真实 h3 生成(<!-- AUTO:cards -->),
|
|||
|
|
这里只负责:搜索过滤、已读进度、展开/收起、键盘快捷键。
|
|||
|
|
========================================================================== */
|
|||
|
|
(function () {
|
|||
|
|
"use strict";
|
|||
|
|
var KEY = "nginx-kb-v1:read";
|
|||
|
|
|
|||
|
|
function $(s, r) { return (r || document).querySelector(s); }
|
|||
|
|
function $$(s, r) { return Array.prototype.slice.call((r || document).querySelectorAll(s)); }
|
|||
|
|
|
|||
|
|
var cards = $$(".chapcard");
|
|||
|
|
|
|||
|
|
/* ---------------- 已读进度(localStorage) ---------------- */
|
|||
|
|
function load() {
|
|||
|
|
try { return JSON.parse(localStorage.getItem(KEY) || "{}"); } catch (e) { return {}; }
|
|||
|
|
}
|
|||
|
|
function save(o) { try { localStorage.setItem(KEY, JSON.stringify(o)); } catch (e) {} }
|
|||
|
|
|
|||
|
|
function paint() {
|
|||
|
|
var done = 0;
|
|||
|
|
cards.forEach(function (c) {
|
|||
|
|
var chk = $("input[type=checkbox]", c);
|
|||
|
|
var on = chk && chk.checked;
|
|||
|
|
c.classList.toggle("done-shown", !!on);
|
|||
|
|
if (on) done++;
|
|||
|
|
});
|
|||
|
|
var pct = cards.length ? Math.round(done / cards.length * 100) : 0;
|
|||
|
|
var bar = $("#bar"), lbl = $("#pct");
|
|||
|
|
if (bar) bar.style.width = pct + "%";
|
|||
|
|
if (lbl) lbl.textContent = pct + "%";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function initChecks() {
|
|||
|
|
var st = load();
|
|||
|
|
cards.forEach(function (c) {
|
|||
|
|
var chk = $("input[type=checkbox]", c);
|
|||
|
|
if (!chk) return;
|
|||
|
|
chk.checked = !!st[chk.dataset.ch];
|
|||
|
|
chk.addEventListener("change", function () {
|
|||
|
|
var s = load();
|
|||
|
|
s[chk.dataset.ch] = chk.checked;
|
|||
|
|
save(s);
|
|||
|
|
paint();
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
paint();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* ---------------- 搜索过滤(顶栏 hero 与目录区共用) ---------------- */
|
|||
|
|
function filter(kw) {
|
|||
|
|
kw = (kw || "").trim().toLowerCase();
|
|||
|
|
var hit = 0;
|
|||
|
|
cards.forEach(function (c) {
|
|||
|
|
var text = (c.dataset.search || c.textContent).toLowerCase();
|
|||
|
|
var on = !kw || text.indexOf(kw) >= 0;
|
|||
|
|
c.classList.toggle("hide", !on);
|
|||
|
|
if (on) hit++;
|
|||
|
|
// 搜索时自动展开小节,方便直接定位
|
|||
|
|
var d = $("details", c);
|
|||
|
|
if (d) d.open = !!kw && on;
|
|||
|
|
});
|
|||
|
|
var none = $("#noHit");
|
|||
|
|
if (none) none.style.display = hit ? "none" : "block";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function bindSearch(el) {
|
|||
|
|
if (!el) return;
|
|||
|
|
var t = null;
|
|||
|
|
el.addEventListener("input", function () {
|
|||
|
|
clearTimeout(t);
|
|||
|
|
t = setTimeout(function () {
|
|||
|
|
// 两个输入框保持同步
|
|||
|
|
$$("#q, #q2").forEach(function (o) { if (o !== el) o.value = el.value; });
|
|||
|
|
filter(el.value);
|
|||
|
|
}, 120);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function initSearch() {
|
|||
|
|
bindSearch($("#q"));
|
|||
|
|
bindSearch($("#q2"));
|
|||
|
|
document.addEventListener("keydown", function (e) {
|
|||
|
|
var q = $("#q");
|
|||
|
|
if (!q) return;
|
|||
|
|
if (e.key === "/" && document.activeElement !== q && document.activeElement !== $("#q2")) {
|
|||
|
|
e.preventDefault(); q.focus();
|
|||
|
|
}
|
|||
|
|
if (e.key === "Escape" && (document.activeElement === q || document.activeElement === $("#q2"))) {
|
|||
|
|
$$("#q, #q2").forEach(function (o) { o.value = ""; });
|
|||
|
|
filter(""); q.blur();
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* ---------------- 工具栏 ---------------- */
|
|||
|
|
function initTools() {
|
|||
|
|
var ex = $("#expandAll");
|
|||
|
|
if (ex) ex.addEventListener("click", function () {
|
|||
|
|
var anyClosed = $$("details", document).some(function (d) { return !d.open; });
|
|||
|
|
$$("details", document).forEach(function (d) { d.open = anyClosed; });
|
|||
|
|
ex.textContent = anyClosed ? "收起全部小节" : "展开全部小节";
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
var rs = $("#resetBtn");
|
|||
|
|
if (rs) rs.addEventListener("click", function () {
|
|||
|
|
if (!confirm("清空所有「已读」勾选?")) return;
|
|||
|
|
save({});
|
|||
|
|
cards.forEach(function (c) { var k = $("input[type=checkbox]", c); if (k) k.checked = false; });
|
|||
|
|
paint();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
var pb = $("#printBtn");
|
|||
|
|
if (pb) pb.addEventListener("click", function () { window.print(); });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function init() {
|
|||
|
|
initChecks();
|
|||
|
|
initSearch();
|
|||
|
|
initTools();
|
|||
|
|
}
|
|||
|
|
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init);
|
|||
|
|
else init();
|
|||
|
|
})();
|
|||
|
|
</script>
|
|||
|
|
</body>
|
|||
|
|
</html>
|