本文及资源最后更新时间 2022-12-13 by sky995
一、安装 Nginx Helper 插件
Enable Nginx Map.
选项保存,即可看到插件为我们生成的映射关系文件信息。
二、修改网站配置文件
以宝塔面板为例,在网站配置文件server
区域上方添加下方代码,并修改引入 Nginx Helper 插件生成的映射关系文件。
这是子目录的:
复制
<code><span class="pln">map $uri $blogname </span><span class="pun">{</span> <span class="pun">~^(?<</span><span class="pln">blogpath</span><span class="pun">></span><span class="str">/[^/]+/</span><span class="pun">)</span><span class="pln">files</span><span class="pun">/(.*)</span><span class="pln"> $blogpath </span><span class="pun">;</span> <span class="pun">}</span><span class="pln"> map $blogname $blogid </span><span class="pun">{</span> <span class="kwd">default</span> <span class="pun">-</span><span class="lit">999</span><span class="pun">;</span><span class="pln"> include </span><span class="pun">映射文件路径.</span><span class="pln">conf</span><span class="pun">;</span> <span class="pun">}</span></code>
这是子域名的:
复制
<code><span class="pln">map $http_host $blogid </span><span class="pun">{</span> <span class="kwd">default</span> <span class="pun">-</span><span class="lit">999</span><span class="pun">;</span><span class="pln"> include </span><span class="pun">映射文件路径.</span><span class="pln">conf</span><span class="pun">;</span> <span class="pun">}</span></code>
保存完成后应如下图所示:
三、设置 Nginx 伪静态规则
还是以宝塔面板为例,打开网站的伪静态配置页面,输入下方代码:
这是子目录的:
复制
<code><span class="com"># 避免 PHP 读取静态文件</span><span class="pln"> location </span><span class="pun">^~</span> <span class="pun">/</span><span class="pln">blogs</span><span class="pun">.</span><span class="pln">dir </span><span class="pun">{</span> <span class="kwd">internal</span><span class="pun">;</span> <span class="kwd">alias</span> <span class="pun">/网站目录/</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">content</span><span class="pun">/</span><span class="pln">blogs</span><span class="pun">.</span><span class="pln">dir </span><span class="pun">;</span><span class="pln"> access_log off</span><span class="pun">;</span><span class="pln"> log_not_found off</span><span class="pun">;</span><span class="pln"> expires max</span><span class="pun">;</span> <span class="pun">}</span> <span class="com"># 通过 map 读取对应站点的文件</span><span class="pln"> location </span><span class="pun">~</span> <span class="pun">^(/[^/]+/)?</span><span class="pln">files</span><span class="pun">/(.+)</span> <span class="pun">{</span><span class="pln"> try_files </span><span class="pun">/</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">content</span><span class="pun">/</span><span class="pln">blogs</span><span class="pun">.</span><span class="pln">dir</span><span class="pun">/</span><span class="pln">$blogid</span><span class="pun">/</span><span class="pln">files</span><span class="pun">/</span><span class="pln">$2 </span><span class="pun">/</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">includes</span><span class="pun">/</span><span class="pln">ms</span><span class="pun">-</span><span class="pln">files</span><span class="pun">.</span><span class="pln">php</span><span class="pun">?</span><span class="pln">file</span><span class="pun">=</span><span class="pln">$2 </span><span class="pun">;</span><span class="pln"> access_log off</span><span class="pun">;</span><span class="pln"> log_not_found off</span><span class="pun">;</span><span class="pln"> expires max</span><span class="pun">;</span> <span class="pun">}</span> <span class="com"># 重写</span> <span class="kwd">if</span> <span class="pun">(!-</span><span class="pln">e $request_filename</span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> rewrite </span><span class="pun">/</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">admin$ $scheme</span><span class="pun">:</span><span class="com">//$host$request_uri/ permanent;</span><span class="pln"> rewrite </span><span class="pun">^(/[^/]+)?(/</span><span class="pln">wp</span><span class="pun">-.*)</span><span class="pln"> $2 </span><span class="kwd">last</span><span class="pun">;</span><span class="pln"> rewrite </span><span class="pun">^(/[^/]+)?(/.*</span><span class="pln">\.php</span><span class="pun">)</span><span class="pln"> $2 </span><span class="kwd">last</span><span class="pun">;</span> <span class="pun">}</span> <span class="com"># 重写</span><span class="pln"> location </span><span class="pun">/</span> <span class="pun">{</span><span class="pln"> try_files $uri $uri</span><span class="pun">/</span> <span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">?</span><span class="pln">$args </span><span class="pun">;</span> <span class="pun">}</span></code>
这是子域名的:
复制
<code><span class="com"># 避免 PHP 读取静态文件</span><span class="pln"> location </span><span class="pun">^~</span> <span class="pun">/</span><span class="pln">blogs</span><span class="pun">.</span><span class="pln">dir </span><span class="pun">{</span> <span class="kwd">internal</span><span class="pun">;</span> <span class="kwd">alias</span> <span class="pun">/网站目录/</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">content</span><span class="pun">/</span><span class="pln">blogs</span><span class="pun">.</span><span class="pln">dir </span><span class="pun">;</span><span class="pln"> access_log off</span><span class="pun">;</span><span class="pln"> log_not_found off</span><span class="pun">;</span><span class="pln"> expires max</span><span class="pun">;</span> <span class="pun">}</span> <span class="com"># 通过 map 读取对应站点的文件</span><span class="pln"> location </span><span class="pun">~</span> <span class="pun">^</span><span class="str">/files/</span><span class="pun">(.*)</span><span class="pln">$ </span><span class="pun">{</span><span class="pln"> try_files </span><span class="pun">/</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">content</span><span class="pun">/</span><span class="pln">blogs</span><span class="pun">.</span><span class="pln">dir</span><span class="pun">/</span><span class="pln">$blogid</span><span class="pun">/</span><span class="pln">$uri </span><span class="pun">/</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">includes</span><span class="pun">/</span><span class="pln">ms</span><span class="pun">-</span><span class="pln">files</span><span class="pun">.</span><span class="pln">php</span><span class="pun">?</span><span class="pln">file</span><span class="pun">=</span><span class="pln">$1 </span><span class="pun">;</span><span class="pln"> access_log off</span><span class="pun">;</span><span class="pln"> log_not_found off</span><span class="pun">;</span><span class="pln"> expires max</span><span class="pun">;</span> <span class="pun">}</span> <span class="com"># 重写</span><span class="pln"> location </span><span class="pun">/</span> <span class="pun">{</span><span class="pln"> try_files $uri $uri</span><span class="pun">/</span> <span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">?</span><span class="pln">$args </span><span class="pun">;</span> <span class="pun">}</span></code>
四、大功告成
如果你有使用 WP-Super-Cache 或者 W3 Total Cache ,可前往https://wordpress.org/support/article/nginx/#wp-super-cache-rules以获取它们所使用的 Nginx 规则,替换上述规则相应的部分,这将使缓存绕过PHP以达到性能最大化。