576 lines
502 KiB
HTML
576 lines
502 KiB
HTML
|
<!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&family=Noto Serif Simplified Chinese:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&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 0 100
|
|||
|
<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>
|
|||
|
<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>缓存是一种存储技术,它保存数据副本以便快速访问。在 Web 环境中,缓存可以是页面、图片、文件等内容,存储在服务器或客户端。当用户再次请求相同的内容时,系统可以直接从缓存中提取,避免了重复的数据检索或计算过程,从而加快了交付速度。</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>
|
|||
|
<ul>
|
|||
|
<li><strong>减轻服务器负担:</strong> 通过缓存常请求的内容,减少对后端服务器的请求次数。</li>
|
|||
|
<li><strong>提高响应速度:</strong> 缓存的内容直接从内存或快速存储中提供,比从后端服务器处理和传输要快得多。</li>
|
|||
|
<li><strong>提升用户体验:</strong> 快速加载的页面和资源提升了用户的浏览体验。</li>
|
|||
|
</ul>
|
|||
|
<h2 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></h2>
|
|||
|
<p>当用户首次请求网站的某个页面时,Nginx 会将请求转发到后端服务器(例如你的网站应用)。后端服务器处理请求后,将页面内容等资源返回给 Nginx,Nginx 再将这些内容发送给用户。在这个过程中,Nginx 会将收到的内容保存在缓存中。</p>
|
|||
|
<p>当另一个用户请求同一个页面时,Nginx 会检查缓存。如果找到了之前保存的内容,并且内容还没有过期,Nginx 就会直接从缓存中取出内容发送给用户,而不是再次请求后端服务器。这样大大减少了处理时间和服务器负载。</p>
|
|||
|
<p>Nginx 不是随意缓存所有内容,它通过一套规则来确定哪些内容应该被缓存,哪些不应该。这套规则可以非常灵活,你可以指定根据页面类型、用户的某些行为,甚至特定时间和日期来缓存内容。</p>
|
|||
|
<p>缓存的内容并不是永远有效的。有时候网站内容会更新,如果用户还是接收到旧的缓存内容,那就会出现问题。因此,Nginx 允许设置缓存的有效期,一旦超过这个期限,缓存的内容就会被认为是过时的,Nginx 会从后端服务器获取最新的内容,并更新缓存。</p>
|
|||
|
<pre><code class="mermaid">sequenceDiagram
|
|||
|
participant 用户
|
|||
|
participant Nginx
|
|||
|
participant 缓存
|
|||
|
participant 后端服务器
|
|||
|
|
|||
|
用户->>Nginx: 发起请求
|
|||
|
alt 缓存存在且有效
|
|||
|
Nginx->>缓存: 检查请求对应的缓存
|
|||
|
缓存->>Nginx: 返回缓存内容
|
|||
|
Nginx->>用户: 提供缓存内容
|
|||
|
else 缓存不存在或过期
|
|||
|
Nginx->>缓存: 检查请求对应的缓存
|
|||
|
缓存->>Nginx: 无有效缓存
|
|||
|
Nginx->>后端服务器: 转发请求
|
|||
|
后端服务器->>Nginx: 处理请求并返回结果
|
|||
|
Nginx->>缓存: 保存新的缓存内容
|
|||
|
Nginx->>用户: 提供后端服务器内容
|
|||
|
end
|
|||
|
</code></pre>
|
|||
|
<h2 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></h2>
|
|||
|
<h3 id="1-配置缓存区域">1. 配置缓存区域<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#1-配置缓存区域" 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 配置文件中定义一个或多个缓存区域:</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;"> proxy_cache_path </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>配置指令</th><th>描述</th></tr></thead><tbody><tr><td><code>proxy_cache_path</code></td><td>指定缓存的存储路径,定义了缓存文件存放的文件系统路径。</td></tr><tr><td><code>levels</code></td><td>定义缓存目录的层级,用于创建多级目录结构来存储缓存文件。</td></tr><tr><td><code>keys_zone</code></td><td>定义缓存区域的名称和大小,为缓存键和元数据分配内存。</td></tr><tr><td><code>max_size</code></td><td>缓存可以占用的最大磁盘空间,限制缓存目录可以使用的最大空间大小。</td></tr><tr><td><code>inactive</code></td><td>在此时间内未被访问的缓存内容将被删除,用于清理长时间未访问的缓存。</td></tr><tr><td><code>use_temp_path</code></td><td>是否使用临时路径存储缓存内容,控制是否先写入临时文件再移动到缓存路径。</td></tr></tbody></table></div>
|
|||
|
<h3 id="2-启用缓存">2. 启用缓存<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#2-启用缓存" 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>在适当的位置块中,启用缓存并指定缓存区域:</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;"> / </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> proxy_cache </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">my_cache;</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_backend;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>配置指令</th><th>描述</th></tr></thead><tbody><tr><td><code>proxy_cache</code></td><td>在 location 块中使用,指定该位置的请求应使用哪个缓存区域。</td></tr><tr><td><code>proxy_cache_key</code></td><td>定义如何为请求生成缓存键,通常包含请求的元素如 URL、请求方法等。</td></tr><tr><td><code>proxy_cache_valid</code></td><td>设置不同 HTTP 状态码的响应的缓存有效期。</td></tr><tr><td><code>proxy_cache_use_stale</code></td><td>允许在后端服务器不可用时提供过期的缓存内容。</td></tr><tr><td><code>proxy_ignore_headers</code></td><td>指示 Nginx 忽略后端服务器发送的某些 HTTP 头,常用于控制缓存。</td></tr><tr><td><code>proxy_cache_lock</code></td><td>在处理缓存项时防止对同一个资源的并发请求,只让第一个请求查询后端并缓存,其他请求等待。</td></tr><tr><td><code>proxy_cache_bypass</code></td><td>设置条件,在满足条件时请求将绕过缓存直接转发给后端服务器。</td></tr><tr><td><code>proxy_no_cache</code></td><td>设置条件,在满足条件时不对响应进行缓存。</td></tr><tr><td><code>proxy_cache_revalidate</code></td><td>每次请求都会验证缓存内容的新鲜度,适用于需要高度一致性的场景。</td></tr><tr><td><code>proxy_cache_min_uses</code></td><td>指定一个响应在被缓存前需要被请求的最小次数,适用于控制不经常访问的内容的缓存。</td></tr><tr><td><code>proxy_cache_methods</code></td><td>指定哪些请求方法的响应应该被缓存,默认是 GET 和 HEAD。</td></tr></tbody></table></div>
|
|||
|
<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;">http</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;"> proxy_cache_path </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;</span></span>
|
|||
|
<span data-line> </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>
|
|||
|
<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:#D73A49;--shiki-dark:#F97583;"> proxy_cache </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">my_cache;</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;"> proxy_cache_key </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">scheme</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">request_method</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">host</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">request_uri</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</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;"> proxy_cache_valid </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">200</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 302</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 10m</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> proxy_cache_valid </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">404</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1m</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;"> proxy_cache_use_stale </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">error</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> timeout updating http_500 http_502 http_503 http_504;</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 忽略后端的Cache-Control和Expires头</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> proxy_ignore_headers </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Cache-Control Expires;</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;"> proxy_cache_lock </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</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;"> proxy_cache_bypass </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$http_cache_control;</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;"> proxy_no_cache </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$http_pragma;</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;"> proxy_cache_revalidate </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</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;"> proxy_cache_min_uses </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</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;"> proxy_cache_methods </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">GET HEAD POST;</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;"> proxy_pass </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">http://my_backend;</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;"> add_header </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">X-Cache-Status $upstream_cache_status;</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>
|
|||
|
<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>合理的浏览器缓存策略可以减少服务器负载、减少网络延迟、加快页面加载速度,并节省用户的数据使用量。浏览器缓存主要通过 HTTP 响应头来控制,以下是一些最常用的 HTTP 缓存控制头:</p>
|
|||
|
<ul>
|
|||
|
<li><strong>Cache-Control</strong>:这是最重要的缓存控制头。它允许指定多种指令来控制资源的缓存策略,例如 <code>max-age</code>、<code>no-cache</code>、<code>no-store</code>、<code>must-revalidate</code> 等。</li>
|
|||
|
<li><strong>Expires</strong>:一个日期/时间值,告诉浏览器在这个时间点之前可以直接从缓存读取资源,而不需要去服务器检查。</li>
|
|||
|
<li><strong>ETag</strong>:资源的唯一标识符。浏览器可以通过发送带有 <code>If-None-Match</code> 的请求来检查 ETag,判断资源是否已更改。</li>
|
|||
|
<li><strong>Last-Modified</strong>:表示资源最后修改的日期和时间。浏览器可以通过发送带有 <code>If-Modified-Since</code> 的请求来检查资源是否已更改。</li>
|
|||
|
</ul>
|
|||
|
<h3 id="cache-control">Cache-Control<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#cache-control" 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>Cache-Control</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:#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;"> \.(js|css|png|jpg|gif)$ </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> add_header </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Cache-Control public,max-age=31536000;</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;"> \.(js|css|png|jpg|gif)$ </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> add_header </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Cache-Control no-store,no-cache,must-revalidate;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>配置选项</th><th>描述</th></tr></thead><tbody><tr><td><code>no-store</code></td><td>指示请求或响应中的信息不能被缓存到任何地方。此设置适用于包含敏感信息的数据。</td></tr><tr><td><code>no-cache</code></td><td>缓存不应该存储未经验证的响应。即使有缓存,每次请求都需要向源服务器进行验证。</td></tr><tr><td><code>public</code></td><td>表示任何响应都可以被缓存,即使是通常不可缓存的内容,如经过身份验证的响应。</td></tr><tr><td><code>private</code></td><td>响应是为单个用户私有的,不能由共享缓存存储,但可以由私有缓存存储。比如,用户的个人信息页。</td></tr><tr><td><code>max-age=<seconds></code></td><td>指定资源可以在本地缓存存储并被重用的最长时间(以秒为单位)。</td></tr><tr><td><code>s-maxage=<seconds></code></td><td>类似于 <code>max-age</code>,但它仅适用于共享缓存(如 CDN)。覆盖 <code>max-age</code> 或者 <code>Expires</code> 头,但仅在共享缓存中。</td></tr><tr><td><code>must-revalidate</code></td><td>一旦资源过期(即超过 <code>max-age</code>),在使用旧的资源之前,缓存必须去服务器验证。对于某些需要保持最新的资源很有用。</td></tr><tr><td><code>proxy-revalidate</code></td><td>与 <code>must-revalidate</code> 类似,但它仅适用于共享缓存。</td></tr><tr><td><code>immutable</code></td><td>指示响应体不会随时间改变。在资源的缓存有效期内,浏览器不需要为了验证资源的新鲜度而发送条件请求。</td></tr><tr><td><code>stale-while-revalidate=<seconds></code></td><td>表示客户端愿意接受超过 <code>max-age</code> 时间的响应,同时在后台异步检查新鲜度。</td></tr><tr><td><code>stale-if-error=<seconds></code></td><td>表示如果源服务器在指定的时间内出错(如服务器宕机),客户端愿意接受一个过期的响应。</td></tr></tbody></table></div>
|
|||
|
<h3 id="expires">Expires<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#expires" 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>Expires</code> 头是 HTTP/1.0 的遗留特性,但在 HTTP/1.1 中仍被广泛支持和使用。它用于指定资源的过期时间,告诉浏览器在这个时间点之前可以直接从本地缓存获取资源,而不必去服务器再次请求。一旦过了这个时间,浏览器会向服务器发送请求以检查资源是否有更新。<strong>尽管 <code>Cache-Control</code> 的 <code>max-age</code> 指令通常优先于 <code>Expires</code>,但在一些老旧的浏览器中,可能仍然需要设置此项。</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:#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>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> /images/ </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;">/path/to/images;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> expires </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">max</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>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> /css/ </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;">/path/to/css;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> expires </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">30d</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>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> location</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> /js/ </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;">/path/to/js;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> expires </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">24h</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>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>时间单位</th><th>描述</th><th>示例</th><th>说明</th></tr></thead><tbody><tr><td><code>max</code></td><td>未来</td><td><code>expires max;</code></td><td>通常被解释为将资源的过期时间设置远在未来的时间点。</td></tr><tr><td><code>y</code></td><td>年</td><td><code>expires 1y;</code></td><td>表示资源应在一年后过期。</td></tr><tr><td><code>M</code></td><td>月</td><td><code>expires 1M;</code></td><td>表示资源应在一个月后过期。</td></tr><tr><td><code>w</code></td><td>周</td><td><code>expires 1w;</code></td><td>表示资源应在一周后过期。</td></tr><tr><td><code>d</code></td><td>天</td><td><code>expires 30d;</code></td><td>表示资源应在 30 天后过期。</td></tr><tr><td><code>h</code></td><td>小时</td><td><code>expires 24h;</code></td><td>表示资源应在 24 小时后过期。</td></tr><tr><td><code>m</code></td><td>分钟</td><td><code>expires 45m;</code></td><td>表示资源应在 45 分钟后过期。</td></tr><tr><td><code>s</code></td><td>秒</td><td><code>expires 30s;</code></td><td>表示资源应在 30 秒后过期。</td></tr></tbody></table></div>
|
|||
|
<h3 id="last-modified">Last-Modified<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#last-modified" 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>Last-Modified</code> 头表示资源最后被修改的时间。它是一个日期/时间戳,告诉浏览器资源最后一次更改的时间。Nginx 默认会为静态文件发送 <code>Last-Modified</code> 头。如果你的资源是静态文件,比如图片、CSS 或 JavaScript 文件,通常你不需要做任何特别的配置。</p>
|
|||
|
<p>当浏览器第一次请求一个资源时,服务器响应中会包含 <code>Last-Modified</code> 头。浏览器在下次请求相同资源时,会发送一个 <code>If-Modified-Since</code> 头,其值为之前收到的 <code>Last-Modified</code> 日期。</p>
|
|||
|
<p>如果服务器发现资源自那以后没有被修改,它会返回一个 304 Not Modified 状态码,而不是资源内容,告诉浏览器可以安全地使用缓存的版本。</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;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</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;">/path/to/your/files;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> add_header </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Last-Modified $date_gmt;</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="etag">ETag<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#etag" 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>ETag</code>(实体标签)提供了一种资源的唯一标识符,通常是一个散列值或版本号。与 <code>Last-Modified</code> 相比,它可以更精确地判断资源是否变化。当浏览器第一次请求一个资源时,如果服务器配置了 ETag,响应中会包含一个 ETag 头。浏览器在后续请求相同资源时会发送一个 <code>If-None-Match</code> 头,其值为之前收到的 ETag 值。</p>
|
|||
|
<p>如果服务器发现 ETag 匹配(即资源未更改),它会返回一个 304 Not Modified 状态码,告诉浏览器可以安全地使用缓存的版本。</p>
|
|||
|
<p>Nginx 不会默认发送 ETag 头,你需要使用第三方模块或自定义配置。以下是一个示例配置,使用 Nginx 的 <code>ngx_http_headers_module</code> 模块来添加 ETag。</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;"> server</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</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;">/path/to/your/files;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> etag </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;"># 开启ETag</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>
|
|||
|
<h2 id="gzip-压缩配置">Gzip 压缩配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gzip-压缩配置" 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><code>gzip</code> 是一种数据格式和文件压缩程序,使用 Lempel-Ziv 编码(LZ77)和 32 位循环冗余校验。当 Web 服务器启用了 <code>gzip</code> 压缩时,它会在发送文件到浏览器之前先将其压缩。支持 <code>gzip</code> 的浏览器会在 HTTP 请求头中包含 <code>Accept-Encoding: gzip</code>,表示它可以接收 <code>gzip</code> 压缩的内容。服务器响应这些请求时,会添加 <code>Content-Encoding: gzip</code> 头部,并发送压缩后的内容。</p>
|
|||
|
<ul>
|
|||
|
<li><strong>减少文件大小</strong>:通常可以减少 70% 左右的文件大小,从而减少网络传输时间。</li>
|
|||
|
<li><strong>提高加载速度</strong>:较小的文件可以更快地下载和解析,提升用户体验。</li>
|
|||
|
<li><strong>节省带宽资源</strong>:对于网站运营者来说,减少数据传输可以降低带宽成本。</li>
|
|||
|
</ul>
|
|||
|
<h3 id="nginx-中配置-gzip-压缩">Nginx 中配置 Gzip 压缩<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#nginx-中配置-gzip-压缩" 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>gzip</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;"> gzip </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_buffers </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">16</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 8k</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_comp_level </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_min_length </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">256</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_http_version </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1.1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_proxied </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">any;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_types </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_vary </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</span><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>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>指令</th><th>描述</th><th>默认值/示例</th></tr></thead><tbody><tr><td><code>gzip</code></td><td>启用或禁用 gzip 压缩。</td><td><code>on</code> / <code>off</code></td></tr><tr><td><code>gzip_buffers</code></td><td>设置用于压缩的缓冲区数量和大小。</td><td><code>16 8k</code></td></tr><tr><td><code>gzip_comp_level</code></td><td>设置 gzip 压缩级别,范围是 1(最快)到 9(最优)。较高的压缩级别可以提供更好的压缩率,但会消耗更多 CPU 资源。</td><td><code>1</code></td></tr><tr><td><code>gzip_disable</code></td><td>禁用 gzip 压缩的浏览器正则匹配。用于兼容不支持 gzip 的老旧浏览器。</td><td><code>"msie6"</code></td></tr><tr><td><code>gzip_min_length</code></td><td>设置触发压缩的最小响应体大小。较小的文件可能不值得压缩。</td><td><code>20</code> 字节</td></tr><tr><td><code>gzip_http_version</code></td><td>设置启用压缩的最低 HTTP 协议版本。</td><td><code>1.1</code></td></tr><tr><td><code>gzip_proxied</code></td><td>设置在何种代理请求情况下应用压缩。</td><td><code>off</code></td></tr><tr><td><code>gzip_types</code></td><td>设置哪些 MIME 类型应该被压缩。通常包括文本文件如 HTML, CSS, JavaScript 等。</td><td><code>text/html</code></td></tr><tr><td><code>gzip_vary</code></td><td>启用或禁用根据请求头中的 <code>Accept-Encoding</code> 发送 <code>Vary: Accept-Encoding</code> 响应头。</td><td><code>off</code></td></tr></tbody></table></div>
|
|||
|
<h3 id="gzip-压缩适合哪些内容">Gzip 压缩适合哪些内容<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gzip-压缩适合哪些内容" 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><strong>文本文件</strong>:HTML、CSS、JavaScript、XML 和 JSON 等文本文件通常能获得很好的压缩效果。</li>
|
|||
|
<li><strong>API 响应</strong>:对 JSON 或 XML 格式的 API 响应进行压缩,能加快移动应用或 Web 服务的响应速度。</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>
|
|||
|
<ul>
|
|||
|
<li><strong>不要压缩已经压缩过的文件</strong>:对于 JPEG、PNG、MP3 和 PDF 等已经压缩过的文件,再次压缩不仅效果不佳,甚至可能使文件变大。</li>
|
|||
|
<li><strong>考虑 CPU 资源</strong>:虽然 <code>gzip</code> 压缩可以减少带宽使用,但它会增加服务器端的 CPU 负担。在负载较高的服务器上,应合理设置压缩级别。</li>
|
|||
|
<li><strong>客户端支持</strong>:几乎所有现代浏览器都支持 <code>gzip</code>,但在配置服务器时仍需考虑兼容性问题。</li>
|
|||
|
</ul>
|
|||
|
<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>
|
|||
|
<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>缓存预热是指在缓存启动后,系统自动访问特定的 URL 列表以填充缓存的过程。这确保了当真实用户请求到达时,他们需要的内容已经在缓存中可用,从而避免了冷启动时的延迟。</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>内容无效化是指在内容更新或变得不再有效时,有选择性地移除或更新缓存中的这些内容。这可以通过设置适当的缓存过期策略或使用 API 调用来手动清除特定的缓存项来实现。</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>根据内容的类型和访问频率,智能缓存层可以自动决定将内容缓存在哪个层级(内存、SSD、HDD 等)。对于经常访问的热门内容,应优先存储在速度更快的存储中。</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>
|
|||
|
<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><strong>精细化缓存键</strong>:调整 <code>proxy_cache_key</code>,确保相似请求共享缓存,不同请求分开缓存,提高缓存利用率。</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>
|
|||
|
<ul>
|
|||
|
<li><strong>使用微缓存</strong>:对动态内容使用极短时间的缓存(例如 1 秒),可以在不牺牲实时性的情况下减轻后端负载。</li>
|
|||
|
<li><strong>内容分发网络(CDN)</strong>:将静态内容部署到 CDN 可以加速全球访问速度,并减轻主服务器的压力。</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>
|
|||
|
<ul>
|
|||
|
<li><strong>用户敏感数据处理</strong>:确保不会将含有用户敏感信息的动态内容错误地缓存。</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>
|
|||
|
<ul>
|
|||
|
<li><strong>最小化资源</strong>:在服务器端压缩和最小化 CSS、JS 等文件,可以减少传输的数据量。</li>
|
|||
|
<li><strong>有效的图片格式</strong>:使用 WebP 等现代图片格式替代传统格式,可以在不牺牲质量的前提下大幅减少图片大小。</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 的访问和错误日志,可以获取关于缓存行为的宝贵信息。</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;">log_format </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">cache </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;">upstream_cache_status</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;">] '</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> '"$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">request</span><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;"> '</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> '"$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">http_referer</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">" "$</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">http_user_agent</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</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 cache;</span></span></code></pre></figure>
|
|||
|
<ul>
|
|||
|
<li><strong>缓存状态码:</strong> Nginx 在响应头中提供了 <code>X-Cache-Status</code>,表示缓存命中、未命中或过期等状态。</li>
|
|||
|
</ul>
|
|||
|
<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;">http</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 开启gzip压缩</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 设置最小压缩长度为1000字节,小于此大小的文件不进行压缩</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_min_length </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1000</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 设置gzip缓冲区大小</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_buffers </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">4</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 32k</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 指定哪些MIME类型的响应应该被压缩</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_types </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">text/plain application/x-javascript text/css application/xml;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 当浏览器支持gzip压缩时,添加Vary: Accept-Encoding头</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_vary </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 设置gzip压缩级别,范围是1(最快)到9(最优),推荐6作为一个平衡点</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_comp_level </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">6</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;"> proxy_cache_path </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;</span></span>
|
|||
|
<span data-line> </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>
|
|||
|
<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;"> \.(jpg|jpeg|png|gif|ico|css|js)$ </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 设置静态资源的过期时间为30天</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> expires </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">30d</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;"> access_log </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">off</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 设置Cache-Control头,使浏览器和中间缓存服务器都可以缓存内容</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> add_header </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Cache-Control </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"public"</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>
|
|||
|
<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:#D73A49;--shiki-dark:#F97583;"> proxy_pass </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">http://my_backend;</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;"> proxy_cache </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">my_cache;</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;"> proxy_cache_revalidate </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</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;"> proxy_cache_min_uses </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</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;"> proxy_cache_use_stale </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">error</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> timeout updating http_500 http_502 http_503 http_504;</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;"> proxy_cache_lock </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</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;"> # 为动态内容设置Cache-Control头,指示浏览器和代理服务器应该如何缓存内容</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> add_header </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Cache-Control </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"public, must-revalidate, proxy-revalidate"</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;"> # 启用gzip压缩</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 允许对代理的响应进行gzip压缩</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_proxied </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">any;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 指定哪些MIME类型的响应应该被压缩</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> gzip_types </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;</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></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="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":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="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":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="#nginx-缓存原理" data-for="nginx-缓存原理">Nginx 缓存原理</a></li><li class="depth-0"><a href="#nginx-缓存配置" data-for="nginx-缓存配置">Nginx 缓存配置</a></li><li class="depth-1"><a href="#1-配置缓存区域" data-for="1-配置缓存区域">1. 配置缓存区域</a></li><li class="depth-1"><a href="#2-启用缓存" data-for="2-启用缓存">2. 启用缓存</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="#cache-control" data-for="cache-control">Cache-Control</a></li><li class="depth-1"><a href="#expires" data-for="expires">Expires</a></li><li class="depth-1"><a href="#last-modified" data-for="last-modified">Last-Modified</a></li><li class="depth-1"><a href="#etag" data-for="etag">ETag</a></li><li class="depth-0"><a href="#gzip-压缩配置" data-for="gzip-压缩配置">Gzip 压缩配置</a></li><li class="depth-1"><a href="#nginx-中配置-gzip-压缩" data-for="nginx-中配置-gzip-压缩">Nginx 中配置 Gzip 压缩</a></li><li class="depth-1"><a href="#gzip-压缩适合哪些内容" data-for="gzip-压缩适合哪些内容">Gzip 压缩适合哪些内容</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-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="[{"path":"Personal&qu
|
|||
|
</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>
|