<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cache &#8211; 科技改变生活-雨落星辰</title>
	<atom:link href="https://p1e.cn/html/tag/cache/feed" rel="self" type="application/rss+xml" />
	<link>https://p1e.cn</link>
	<description>所有的伟大,都源于一个勇敢的开始</description>
	<lastBuildDate>Thu, 27 Jan 2022 06:38:05 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>宝塔面板开启Nginx fastcgi_cache缓存为WordPress提速</title>
		<link>https://p1e.cn/html/3099.html</link>
					<comments>https://p1e.cn/html/3099.html#respond</comments>
		
		<dc:creator><![CDATA[Naoki]]></dc:creator>
		<pubDate>Wed, 12 Jan 2022 05:11:39 +0000</pubDate>
				<category><![CDATA[运维笔记]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">https://www.815494.com/?p=3099</guid>

					<description><![CDATA[说到wordpress的缓存，大家想到的肯定是 WP-Super-Cache 的静态html缓存，以及 memcached 或 redis 动态缓存，插件的缓存效果肯定是有的，但是容易出现各种问题，比如配置很复杂、配置全英文、插件之间的冲突等，所以今天我要教大家一个更高级的缓存：Nginx fastcgi_cache缓存，直接在nginx层面缓存页面，还支持缓存伪静态！效果比起传统的php缓存好得太多了，因为很多人使用宝塔面板，所以今天的教程是基于宝塔面板的教程。 全局设置 现在我们开始配置nginx，来到宝塔后台]]></description>
										<content:encoded><![CDATA[<p>说到wordpress的缓存，大家想到的肯定是 <strong>WP-Super-Cache</strong> 的静态html缓存，以及 <strong>memcached</strong> 或 <strong>redis</strong> 动态缓存，插件的缓存效果肯定是有的，但是容易出现各种问题，比如配置很复杂、配置全英文、插件之间的冲突等，所以今天我要教大家一个更高级的缓存：<strong>Nginx fastcgi_cache缓存</strong>，直接在nginx层面缓存页面，还支持缓存伪静态！效果比起传统的php缓存好得太多了，因为很多人使用宝塔面板，所以今天的教程是基于宝塔面板的教程。</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://i.p1e.cn/2022/01/blog20220127010556l5wi5b.png" rel="lightbox[pic]"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3163" src="https://i.p1e.cn/2022/01/blog20220127010556l5wi5b.png" width="680" height="416" /></a></figure>
<h2>全局设置</h2>
<p>现在我们开始配置nginx，来到宝塔后台，在软件商店找到Nginx，点击设置按钮，在配置修改中添加以下内容：</p>
<pre class="wp-block-code"><code>fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
#忽略一切 nocache 申明，避免不缓存伪静态等
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;</code></pre>
<p>加好之后截图如下：</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" class="alignnone size-full wp-image-3103" src="//i.p1e.cn/2022/01/k5sUeU.png" width="650" height="602" /><figcaption>添加全局缓存设置</figcaption></figure>
</div>
<h2>网站设置</h2>
<p>在宝塔后台的网站列表中，找到你的网站，并且点击设置按钮，将以下代码添加到配置文件中去：</p>
<pre class="wp-block-code"><code>set $skip_cache 0;
#post 访问不缓存
if ($request_method = POST) {
	set $skip_cache 1;
}   
#动态查询不缓存
if ($query_string != "") {
	set $skip_cache 1;
}   
#后台等特定页面不缓存（其他需求请自行添加即可）
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
	set $skip_cache 1;
}   
#对登录用户、评论过的用户不展示缓存
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
	set $skip_cache 1;
}
#这里请参考你网站之前的配置，特别是sock的路径，弄错了就502了！如果你的网站使用PHP7.4，就写-74.sock
location ~ [^/]\.php(/|$)
{
    try_files $uri =404;
    fastcgi_pass unix:/tmp/php-cgi-74.sock;
    fastcgi_index index.php;
    include fastcgi.conf;  
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    #新增的缓存规则
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    add_header X-Cache "$upstream_cache_status From $host";
    fastcgi_cache WORDPRESS;
    add_header Cache-Control  max-age=0;
    add_header Nginx-Cache "$upstream_cache_status";
    add_header Last-Modified $date_gmt;
    add_header X-Frame-Options SAMEORIGIN; # 只允许本站用 frame 来嵌套
    add_header X-Content-Type-Options nosniff; # 禁止嗅探文件类型
    add_header X-XSS-Protection "1; mode=block"; # XSS 保护
    etag  on;
    fastcgi_cache_valid 200 301 302 1d;
}
#缓存清理配置
location ~ /purge(/.*) {
	allow 127.0.0.1;
	allow "服务器外网IP"; # 引号要保留
	deny all;
	fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}</code></pre>
<p>加好之后的截图如下：</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><figcaption><img decoding="async" class="alignnone size-full wp-image-3101" src="//i.p1e.cn/2022/01/1641964211-16419626861.png" alt="" width="776" height="546" /><br />
局部站点设置</figcaption></figure>
</div>
<p>添加好之后，重载Nginx设置，缓存就加好了，</p>
<p>按 <strong>F12</strong> 开启开发者工具，在未登录的情况下访问网站首页，查看文件头，如果出现 <strong>HIT</strong> 则是缓存了，<strong>BYPASS</strong> 则是因设置原因未缓存，<strong>MISS</strong> 即这个页面还没被缓存，新发布或刚被删除的页面，首次访问将出现这个状态，如图所示：</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><figcaption><img loading="lazy" decoding="async" class="alignnone size-full wp-image-3102" src="//i.p1e.cn/2022/01/1641964243-16419634931.png" alt="" width="585" height="451" /><br />
缓存命中！</figcaption></figure>
</div>
</div>
<div></div>]]></content:encoded>
					
					<wfw:commentRss>https://p1e.cn/html/3099.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
