1
0
wiki/Technology/ComputerNetwork/Nginx/基础配置.html

754 lines
494 KiB
HTML
Raw Normal View History

2024-09-06 10:58:26 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>基础配置</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="基础配置"/><meta property="og:description" content="Nginx 的基础配置."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../static/icon.png"/><meta name="description" content="Nginx 的基础配置."/><meta name="generator" content="Quartz"/><link href="../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/ComputerNetwork/Nginx/基础配置"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0
<ul>
<li><strong>Linux</strong>:在大多数 Linux 发行版中,可以通过包管理器(如 APT、YUM来安装 Nginx。对于希望有更细粒度控制的高级用户可以选择从源代码编译安装。</li>
<li><strong>Windows</strong>:可从 Nginx 官方网站下载预编译的 Windows 版本进行安装。</li>
<li><strong>MacOS</strong>:通常通过包管理器如 Homebrew 来安装。</li>
</ul>
<h3 id="主要目录和文件">主要目录和文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#主要目录和文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>路径</th><th>类型</th><th>描述</th></tr></thead><tbody><tr><td><code>/etc/nginx/</code>, <code>/etc/nginx/conf.d</code>, <code>/etc/nginx/nginx.conf</code></td><td>目录/配置文件</td><td>存放 Nginx 的主要配置文件。</td></tr><tr><td><code>/etc/nginx/conf.d/default.conf</code></td><td>配置文件</td><td>默认的服务器配置文件。</td></tr><tr><td><code>/etc/logrotate.d/nginx</code></td><td>配置文件</td><td>用于 logrotate 服务的日志轮转配置。</td></tr><tr><td><code>/etc/nginx/{fastcgi_params, scgi_params, uwsgi_params}</code></td><td>配置文件</td><td>特定于 cgi 的配置文件。</td></tr><tr><td><code>/etc/nginx/{koi-utf, koi-win, win-utf}</code></td><td>配置文件</td><td>编码映射转换文件。</td></tr><tr><td><code>/etc/nginx/mime.types</code></td><td>配置文件</td><td>定义 HTTP Content-Type 与文件扩展名的映射。</td></tr><tr><td><code>/etc/sysconfig/nginx</code>, <code>/etc/sysconfig/nginx-debug</code></td><td>配置文件</td><td>配置系统守护进程管理器。</td></tr><tr><td><code>/usr/lib/systemd/system/{nginx.service, nginx-debug.service}</code></td><td>配置文件</td><td>Systemd 服务文件,用于管理 Nginx 服务。</td></tr><tr><td><code>/etc/nginx/modules</code>, <code>/usr/lib64/nginx/modules</code></td><td>目录</td><td>存放 Nginx 模块的目录。</td></tr><tr><td><code>/usr/sbin/{nginx, nginx-debug}</code></td><td>命令</td><td>Nginx 服务的可执行文件。</td></tr><tr><td><code>/usr/share/doc/nginx-1.12.2</code>, <code>/usr/share/man/man8/nginx.8.gz</code></td><td>文件/目录</td><td>包含 Nginx 的手册和帮助文件。</td></tr><tr><td><code>/var/cache/nginx</code></td><td>目录</td><td>Nginx 的缓存目录。</td></tr><tr><td><code>/var/log/nginx</code></td><td>目录</td><td>存放 Nginx 日志的目录。</td></tr><tr><td><code>/usr/share/nginx/html/{404.html, 50x.html, index.html}</code></td><td>文件</td><td>Nginx 默认的静态资源文件。</td></tr></tbody></table></div>
<p>Nginx 及其模块的工作方式由配置文件决定。默认情况下,配置文件名为 <code>nginx.conf</code>,通常位于 <code>/etc/nginx/</code> 或其子目录中。</p>
<h3 id="使用和管理">使用和管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用和管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<div class="table-container"><table><thead><tr><th>功能</th><th>命令</th><th>描述</th></tr></thead><tbody><tr><td>启动 Nginx</td><td><code>sudo systemctl start nginx</code></td><td>在 Linux 系统上启动 Nginx 服务。</td></tr><tr><td>停止 Nginx快速</td><td><code>sudo nginx -s stop</code></td><td>立即终止 Nginx 服务。</td></tr><tr><td>停止 Nginx优雅</td><td><code>sudo nginx -s quit</code></td><td>等待当前处理的请求结束后再终止 Nginx 服务。</td></tr><tr><td>重新加载配置</td><td><code>sudo nginx -s reload</code></td><td>在不中断服务的情况下重新加载配置文件。</td></tr><tr><td>测试配置文件</td><td><code>sudo nginx -t</code></td><td>测试 Nginx 配置文件的语法正确性。</td></tr><tr><td>查看 Nginx 版本</td><td><code>nginx -v</code></td><td>显示已安装的 Nginx 版本。</td></tr><tr><td>查看 Nginx 编译选项</td><td><code>nginx -V</code></td><td>显示 Nginx 编译选项和配置参数。</td></tr><tr><td>重启 Nginx</td><td><code>sudo systemctl restart nginx</code></td><td>在 Linux 系统上重启 Nginx 服务。</td></tr><tr><td>启用 Nginx 开机自启</td><td><code>sudo systemctl enable nginx</code></td><td>设置 Nginx 在系统启动时自动启动(仅限 Linux</td></tr><tr><td>禁用 Nginx 开机自启</td><td><code>sudo systemctl disable nginx</code></td><td>禁止 Nginx 在系统启动时自动启动(仅限 Linux</td></tr></tbody></table></div>
<p><em>注意:如果您的系统开启了防火墙,可能需要配置防火墙规则来允许访问 Nginx 服务的端口。</em></p>
<h2 id="基础配置">基础配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基础配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>Nginx 的配置文件通常位于 <code>/etc/nginx/nginx.conf</code>。它包含几个关键部分:</p>
<ul>
<li><strong>events 块</strong>:配置与 Nginx 服务器及客户端网络连接相关的参数。</li>
<li><strong>http 块</strong>:定义全局 HTTP 服务器配置,例如文件类型、日志格式、默认文件类型等。
<ul>
<li><strong>server 块</strong>:定义虚拟主机及其行为,例如监听的端口和服务器名称。</li>
<li><strong>location 块</strong>:根据请求的 URI 匹配并定义处理特定类型请求的行为。</li>
</ul>
</li>
<li><strong>mail 块</strong>(可选):配置邮件代理服务,如 SMTP、IMAP 和 POP3。</li>
<li><strong>stream 块</strong>(可选):为非 HTTP 协议(例如 TCP 和 UDP提供配置。</li>
</ul>
<h3 id="层级">层级<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#层级" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (1) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Nginx 的全局配置,适用于所有的服务器和连接</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">├── </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">events</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (1) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置影响 Nginx 服务器或与用户的网络连接的参数</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">├── </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">http</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (1) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 用于配置代理、缓存、日志等绝大多数功能和第三方模块,只能定义一个 http 块</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">│ ├── </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (多个) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">│ │ └── location (多个) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置请求路由和各种页面处理情况,每个 server 块可以包含多个 location 块</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">│ └── ... </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 可配置多个 server 块</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">├── mail (1) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置邮件服务(可选)</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">└── stream (1) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 配置流处理(可选)</span></span></code></pre></figure>
<pre><code class="mermaid">graph TD
A[Nginx配置] -->|顶级块| B[events]
A -->|顶级块| C[http]
A -->|顶级块| K[stream]
A -->|顶级块| L[mail]
C -->|子块| D[server]
C -->|子块| J[upstream]
D -->|子块| E[location]
D -->|子块| F[SSL配置]
D -->|子块| G[负载均衡配置]
D -->|监听| H[IPv4端口]
D -->|监听| I[IPv6端口]
K -->|子块| M[server - TCP/UDP]
M -->|监听| N[IPv4端口 - TCP/UDP]
M -->|监听| O[IPv6端口 - TCP/UDP]
</code></pre>
<h3 id="语法">语法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#语法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<ul>
<li>配置文件由指令与指令块构成。</li>
<li>每条指令以分号(<code>;</code>)结尾,指令与参数间以空格符号分隔。</li>
<li>指令块以大括号(<code>{}</code>)将多条指令组织在一起。</li>
<li><code>include</code> 语句允许组合多个配置文件以提升可维护性。</li>
<li>通过 <code>#</code> 符号添加注释,提高可读性。</li>
<li>通过 <code>$</code> 符号使用变量。</li>
<li>部分指令的参数支持正则表达式,例如常用的 location 指令。</li>
</ul>
<h4 id="简单指令">简单指令<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#简单指令" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>简单指令由名称和参数组成,中间<strong>用空格分开,并以分号结束。</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#简单指令</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">root </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/data/www;</span></span></code></pre></figure>
<h4 id="块级指令">块级指令<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#块级指令" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>块级指令结构类似于简单指令,但末尾不是分号,而是用大括号 <code>{}</code> 包裹的额外指令集合。如果一个块级指令中包含其他指令,则 - 配置文件由指令与指令块构成。</p>
<ul>
<li>每条指令以分号(<code>;</code>)结尾,指令与参数间以空格符号分隔。</li>
<li>指令块以大括号(<code>{}</code>)将多条指令组织在一起。</li>
<li><code>include</code> 语句允许组合多个配置文件以提升可维护性。</li>
<li>通过 <code>#</code> 符号添加注释,提高可读性。</li>
<li>通过 <code>$</code> 符号使用变量。</li>
<li>部分指令的参数支持正则表达式,例如常用的 location 指令。它被称为一个上下文例如events、http、server 和 location</li>
</ul>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 块级指令示例</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">http</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> listen </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">80</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server_name </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">example.com;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> access_log </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/path/to/log/example.access.log;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> root </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/path/to/root;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ~</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;"> \.php$ </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> fastcgi_pass </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">127.0.0.1:9000;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<h3 id="匹配规则">匹配规则<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#匹配规则" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p><code>location</code> 指令用于匹配请求 URI忽略查询参数可以使用字符串或正则表达式。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">location</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> [ = | ~ | ~* | ^~ | 空] uri </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ……</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<ul>
<li><code>=</code>:精确匹配。</li>
<li><code>^~</code>:前缀匹配,如果匹配成功,不再进行后续正则匹配。</li>
<li><code>~</code>:区分大小写的正则匹配。</li>
<li><code>~*</code>:不区分大小写的正则匹配。</li>
<li><code></code>:无修饰符的匹配,匹配以 uri 开头的字符串。</li>
</ul>
<h4 id="匹配优先级">匹配优先级<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#匹配优先级" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>优先级</th><th>类型</th><th>描述</th><th>修饰符</th></tr></thead><tbody><tr><td>1</td><td>精确匹配</td><td>完全匹配整个 URI</td><td><code>=</code></td></tr><tr><td>2</td><td>最长非正则前缀匹配</td><td>匹配最长的前缀字符串,但不包括正则表达式</td><td><code>^~</code></td></tr><tr><td>3</td><td>正则表达式匹配</td><td>按定义顺序匹配正则表达式</td><td><code>~</code> (区分大小写) 或 <code>~*</code> (不区分大小写)</td></tr><tr><td>4</td><td>普通匹配</td><td>匹配以指定字符串开始的 URI</td><td>无修饰符</td></tr></tbody></table></div>
<p>请注意以下几点:</p>
<ul>
<li><strong>精确匹配</strong><code>=</code>)具有最高优先级。如果找到匹配项,则立即处理请求并停止搜索。</li>
<li><strong>最长非正则前缀匹配</strong><code>^~</code>)在没有精确匹配时使用,并且如果找到匹配项,将不会检查后续的正则表达式。</li>
<li><strong>正则表达式匹配</strong><code>~</code><code>~*</code>)根据它们在配置文件中出现的顺序进行匹配。第一个成功匹配的正则表达式将停止搜索。</li>
<li><strong>普通匹配</strong>(无修饰符)具有最低的优先级。如果以上所有类型都没有匹配,将使用这个匹配规则。</li>
</ul>
<h4 id="匹配过程示例">匹配过程示例<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#匹配过程示例" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> listen </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">80</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server_name </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">example.com;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 精确匹配</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;"> /exact </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 处理 /exact 精确请求</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 最长非正则前缀匹配</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ^~</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;"> /static/ </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 处理以 /static/ 开头的请求</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 正则表达式匹配</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ~</span><span style="--shiki-light:#032F62;--shiki-dark:#DBEDFF;"> \.php$ </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 处理以 .php 结尾的请求</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 普通匹配</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> / </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 处理所有其他请求</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<pre><code class="mermaid">graph TD;
A[开始匹配] -->|接收请求| B[尝试精确匹配 '=' ];
B -- 匹配成功 --> C[处理请求 '/exact'];
B -- 匹配失败 --> D[尝试最长非正则前缀匹配 '^~'];
D -- 匹配成功 --> E[处理以 '/static/' 开头的请求];
D -- 匹配失败 --> F[尝试正则表达式匹配 '~' 或 '~*'];
F -- 匹配成功 --> G[处理以 '.php' 结尾的请求];
F -- 匹配失败 --> H[尝试普通匹配(无修饰符)];
H -- 匹配成功 --> I[处理所有其他请求];
H -- 匹配失败 --> J[无法处理请求];
</code></pre>
<p>在这个配置和流程图中:</p>
<ul>
<li><strong>精确匹配</strong>Nginx 首先尝试精确匹配 location <code>= /exact</code></li>
<li><strong>最长非正则前缀匹配</strong>如果精确匹配失败Nginx 尝试最长非正则前缀匹配 location <code>^~ /static/</code></li>
<li><strong>正则表达式匹配</strong>如果最长非正则前缀匹配失败Nginx 接着尝试正则表达式匹配 location <code>~ \.php$</code></li>
<li><strong>普通匹配</strong>如果所有正则表达式都不匹配Nginx 最后尝试无修饰符的普通匹配 location <code>/</code></li>
<li><strong>处理请求</strong>根据匹配的类型Nginx 将处理相应的请求。</li>
<li><strong>无法处理请求</strong>:如果所有类型的匹配都失败,请求无法被处理。</li>
</ul>
<h3 id="全局变量">全局变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#全局变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Nginx 提供了多种内置变量,可以在配置的任何地方使用。以下是一些常见的 Nginx 全局变量及其描述:</p>
<h4 id="http-相关变量">HTTP 相关变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#http-相关变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>变量</th><th>描述</th></tr></thead><tbody><tr><td><code>$host</code></td><td>请求行中的 Host如果有 Host 请求头,则用其值替换掉请求行中的主机名。如果请求中没有 Host 行和 Host 请求头,则等于请求匹配的 server 名称。值为小写,不包含端口。</td></tr><tr><td><code>$uri</code></td><td>请求中的当前 URI不包括 <code>?</code> 后面的请求参数,它可以通过内部重定向或使用 index 指令进行修改。</td></tr><tr><td><code>$document_uri</code></td><td>当前请求在 root 指令中指定的值,与 <code>$uri</code> 相同,存在于历史问题中。</td></tr><tr><td><code>$request_uri</code></td><td>完整的原始请求 URL包括 URI 和参数,无法修改。</td></tr><tr><td><code>$scheme</code></td><td>请求模式,如 http 或 https。</td></tr><tr><td><code>$request</code></td><td>原始的 URL 请求,包含方法和协议版本。</td></tr><tr><td><code>$request_method</code></td><td>客户端请求类型,如 GET、POST。</td></tr><tr><td><code>$request_length</code></td><td>所有请求内容的大小,包括请求行、头部、包体等。</td></tr><tr><td><code>$request_body</code></td><td>请求中的包体,当使用反向代理并设定用内存暂存包体时有效。</td></tr><tr><td><code>$request_body_file</code></td><td>临时存放请求包体的文件,可以通过特定指令强制所有包体存入文件。</td></tr><tr><td><code>$remote_user</code></td><td>由 HTTP Basic Authentication 协议传入的用户名。</td></tr><tr><td><code>$args</code></td><td>请求中的参数,可以被修改。</td></tr><tr><td><code>$arg_PARAMETER</code></td><td>GET 请求中特定参数 PARAMETER 的值。</td></tr><tr><td><code>$is_args</code></td><td>如果请求有参数则为 ”?”,否则为空字符串。</td></tr><tr><td><code>$query_string</code></td><td><code>$args</code> 相同,表示请求的查询字符串。</td></tr><tr><td><code>$content_length</code></td><td>请求头中的 Content-Length 字段值。</td></tr><tr><td><code>$content_type</code></td><td>请求头中的 Content-Type 字段值。</td></tr><tr><td><code>$http_HEADER</code></td><td>特定 HTTP 请求头的内容HEADER 需转为小写,破折号变为下划线。</td></tr><tr><td><code>$http_user_agent</code></td><td>客户端的 user-agent 信息。</td></tr><tr><td><code>$http_cookie</code></td><td>客户端的 cookie 信息。</td></tr><tr><td><code>$cookie_COOKIE</code></td><td>获取特定 cookie 的值。</td></tr></tbody></table></div>
<h4 id="tcp-相关变量">TCP 相关变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#tcp-相关变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>变量</th><th>描述</th></tr></thead><tbody><tr><td><code>$binary_remote_addr</code></td><td>客户端地址的整型格式,对于 IPv4 是 4 字节,对于 IPv6 是 16 字节。</td></tr><tr><td><code>$remote_addr</code></td><td>客户端的 IP 地址。</td></tr><tr><td><code>$remote_port</code></td><td>客户端的端口。</td></tr><tr><td><code>$connection</code></td><td>递增的连接序号。</td></tr><tr><td><code>$connection_requests</code></td><td>当前连接上执行过的请求数,对 keepalive 连接有意义。</td></tr><tr><td><code>$proxy_protocol_addr</code></td><td>如果使用了 proxy_protocol 协议,则返回协议中的地址(原始用户的地址),否则返回空。</td></tr><tr><td><code>$proxy_protocol_port</code></td><td>如果使用了 proxy_protocol 协议,则返回协议中的端口(原始用户的端口),否则返回空。</td></tr><tr><td><code>$server_addr</code></td><td>服务器端地址。</td></tr><tr><td><code>$server_port</code></td><td>服务器端端口。</td></tr><tr><td><code>$server_protocol</code></td><td>服务器端协议,例如 HTTP/1.1。</td></tr><tr><td><code>$TCP_INFO</code></td><td>TCP 内核层参数,包括各种 TCP 信息。</td></tr></tbody></table></div>
<h4 id="nginx-处理请求过程中产生的变量">Nginx 处理请求过程中产生的变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#nginx-处理请求过程中产生的变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>变量</th><th>描述</th></tr></thead><tbody><tr><td><code>$request_time</code></td><td>请求处理到现在的耗时,单位为秒,精确到毫秒。</td></tr><tr><td><code>$server_name</code></td><td>匹配上请求的 server_name 值。</td></tr><tr><td><code>$https</code></td><td>如果开启了 TLS/SSL则返回 on否则返回空。</td></tr><tr><td><code>$request_completion</code></td><td>若请求处理完则返回 OK否则返回空。</td></tr><tr><td><code>$request_id</code></td><td>以 16 进制输出的请求标识 id共含有 16 个字节,随机生成。</td></tr><tr><td><code>$request_filename</code></td><td>待访问文件的完整路径。</td></tr><tr><td><code>$document_root</code></td><td>由 URI 和 root/alias 规则生成的文件夹路径。</td></tr><tr><td><code>$realpath_root</code></td><td>将 document_root 中的软链接等换成真实路径。</td></tr><tr><td><code>$limit_rate</code></td><td>客户端响应时的速度上限,单位为每秒字节数。可以通过 <code>set</code> 指令修改。</td></tr></tbody></table></div>
<h4 id="发送-http-响应时相关的变量">发送 HTTP 响应时相关的变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#发送-http-响应时相关的变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>变量</th><th>描述</th></tr></thead><tbody><tr><td><code>$sent_http_HEADER</code></td><td>HTTP 响应头中的内容HEADER 需转为小写,破折号变为下划线。</td></tr><tr><td><code>$status</code></td><td>HTTP 响应状态。</td></tr><tr><td><code>$body_bytes_sent</code></td><td>传送页面的字节数,即响应中 body 包体的长度。</td></tr><tr><td><code>$bytes_sent</code></td><td>全部 HTTP 响应的长度。</td></tr><tr><td><code>$sent_trailer_名字</code></td><td>把响应结尾内容里的值返回。</td></tr></tbody></table></div>
<h4 id="nginx-系统变量">Nginx 系统变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#nginx-系统变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>变量</th><th>描述</th></tr></thead><tbody><tr><td><code>$time_local</code></td><td>以本地时间标准输出的当前时间。</td></tr><tr><td><code>$time_iso8601</code></td><td>使用 ISO 8601 标准输出的当前时间。</td></tr><tr><td><code>$nginx_version</code></td><td>当前运行的 Nginx 版本号。</td></tr><tr><td><code>$pid</code></td><td>所属 worker 进程的 id。</td></tr><tr><td><code>$pipe</code></td><td>如果使用了管道则返回 p否则返回 <code>.</code></td></tr><tr><td><code>$hostname</code></td><td>所在服务器的主机名,与 hostname 命令输出一致。</td></tr><tr><td><code>$msec</code></td><td>从 1970 年 1 月 1 日到现在的时间,单位为秒,小数点后精确到毫秒。</td></tr></tbody></table></div>
<p>Nginx 的配置文件使用的是一门微型的编程语言,其中变量存放的是字符串类型的值。使用 <code>$</code> 符号来表示变量,并且在引用时需要带上 <code>$</code> 前缀。特殊情况下,如变量名紧跟字符时,使用特殊语法来消除歧义。如果需要输出 <code>$</code> 符号本身,则可以使用特定方法,如上述 <code>geo $dollar</code> 示例所示。</p>
<h3 id="模块化配置">模块化配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#模块化配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>在 Nginx 配置中,<code>include</code> 指令允许您包含其他配置文件或片段,实现配置的模块化和简化管理。以下是如何有效使用 <code>include</code> 指令的步骤和示例:</p>
<ol>
<li>
<p><strong>定义共享配置文件</strong>:首先,创建一个新的配置文件来存储可重复使用的配置片段。例如,您可以创建 <code>/etc/nginx/shared_config.conf</code> 并在其中定义通用的 <code>server</code> 块、<code>location</code> 块或其他配置指令。</p>
<p>示例 <code>shared_config.conf</code> 内容:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> listen </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">80</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server_name </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">example.com;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> / </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> root </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/var/www/html;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> index </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">index.html;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
</li>
<li>
<p><strong>在主配置文件中包含共享配置</strong>:在您的主 Nginx 配置文件(通常是 <code>/etc/nginx/nginx.conf</code>)中,使用 <code>include</code> 指令引入共享配置文件。这可以在 <code>http</code> 块或 <code>server</code> 块内进行,取决于您的具体需求。</p>
<p><code>http</code> 块内包含共享配置:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">http</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> include </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/etc/nginx/shared_config.conf;</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 其他 http 块配置...</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<p>或者,在 <code>server</code> 块内包含共享配置:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 其他 server 块配置...</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> include </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/etc/nginx/shared_config.conf;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
</li>
<li>
<p><strong>重新加载或重启 Nginx</strong>:为了使新的配置生效,您需要重新加载或重启 Nginx。通常可以使用以下命令来重新加载配置</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> nginx</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> reload</span></span></code></pre></figure>
</li>
</ol>
<p>通过使用 <code>include</code> 指令,您可以将常用配置抽象成独立的文件,简化主配置文件的复杂度,并提高配置的可维护性。此方法尤其适用于需要在多个虚拟主机或环境之间共享配置的场景。</p>
<h2 id="完整示例">完整示例<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#完整示例" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="nginx" data-theme="github-light github-dark"><code data-language="nginx" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">user </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> nginx; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义Nginx运行的用户</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">worker_processes </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> auto; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 工作进程数建议设置为CPU核心数auto表示自动检测</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">error_log </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /var/log/nginx/error.log </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">warn</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 错误日志文件的位置和记录级别</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">pid </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /var/run/nginx.pid; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 存储Nginx主进程ID的文件位置</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">events</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> worker_connections </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1024</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 单个工作进程的最大连接数</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> multi_accept </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置为on表示一个工作进程可以同时接受多个新连接</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> use </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">epoll</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用epoll事件模型这是在Linux上的一种高效的事件处理机制</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">http</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> include </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> mime.types; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 包含MIME类型设置</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> default_type </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> application/octet-stream;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 日志格式定义,包括自定义变量</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> log_format </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> main</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> '$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">remote_addr</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> - $</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">remote_user</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> [$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">time_local</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">] &quot;$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">request</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot; '</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> '$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">status</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> $</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">body_bytes_sent</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">http_referer</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot; '</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> '&quot;$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">http_user_agent</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot; &quot;$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">http_x_forwarded_for</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 应用自定义日志格式</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> access_log </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /var/log/nginx/access.log </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 文件传输优化</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> sendfile </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 开启高效文件传输模式</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> tcp_nopush </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 开启防止网络拥塞的优化</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> tcp_nodelay </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 开启不延迟发送数据的优化</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 保持长连接的超时时间</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> keepalive_timeout </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 65</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # SSL 配置</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ssl_certificate </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /path/to/cert.pem;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ssl_certificate_key </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/path/to/cert.key;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ssl_protocols </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> TLSv1.2 TLSv1.3;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ssl_ciphers </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> HIGH:!aNULL:!MD5;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 服务器定义</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> listen </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 80</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 监听IPv4端口</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> listen </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [::]:80; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 监听IPv6端口</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server_name </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> localhost; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 服务器名称</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 根目录和索引文件设置</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> / </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> root </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /usr/share/nginx/html; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 网站根目录</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> index </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> index.html index.htm; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 默认页面</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 负载均衡配置</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> /load_balanced_app </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> proxy_pass </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">http://my_load_balancer; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 反向代理到负载均衡器</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # SSL 服务端配置</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> listen </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">443</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ssl;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ssl_certificate </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/path/to/ssl/cert.pem;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ssl_certificate_key </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/path/to/ssl/key.pem;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 负载均衡器定义</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> upstream</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> my_load_balancer </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> backend1.example.com;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> backend2.example.com;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> backend3.example.com;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">stream</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # TCP/UDP流设置示例</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> listen </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">12345</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> proxy_pass </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">backend.example.com:12345;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # IPv6 TCP/UDP 代理</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> listen </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[::]:12345;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> proxy_pass </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">backend.example.com:12345;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">mail</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 邮件服务设置(示例)</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> server_name </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">mail.example.com;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> auth_http </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> localhost/auth_http;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#安装使用" data-for="安装使用">安装使用</a></li><li class="depth-1"><a href="#主要目录和文件" data-for="主要目录和文件">主要目录和文件</a></li><li class="depth-1"><a href="#使用和管理" data-for="使用和管理">使用和管理</a></li><li class="depth-0"><a href="#基础配置" data-for="基础配置">基础配置</a></li><li class="depth-1"><a href="#层级" data-for="层级">层级</a></li><li class="depth-1"><a href="#语法" data-for="语法">语法</a></li><li class="depth-1"><a href="#匹配规则" data-for="匹配规则">匹配规则</a></li><li class="depth-1"><a href="#全局变量" data-for="全局变量">全局变量</a></li><li class="depth-1"><a href="#模块化配置" data-for="模块化配置">模块化配置</a></li><li class="depth-0"><a href="#完整示例" data-for="完整示例">完整示例</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&quot;path&quot;:&quot;Personal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2018&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2020&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2021&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2022&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2023&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2024&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/个人成长&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/医学健康&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/历史&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/哲学宗教&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/心理&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/政治军事&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/教育学习&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/文学&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/生活百科&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/社会文化&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/科学技术&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/经济理财&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/艺术&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/计算机&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Journal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Journal/2022&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Journal/202
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../postscript.js" type="module"></script></html>