欢迎来到好人卡资源网,专注网络技术资源收集,我们不仅是网络资源的搬运工,也生产原创资源。寻找资源请留言

WordPress 站点网络(多站点|子目录、子域名)Nginx 伪静态设置

网站源码 sky995 来源:耗子博客 2个月前 (12-13) 113次浏览 0个评论

本文最后更新时间 2022-12-13 by sky995

Nginx Helper 插件可以帮助我们自动生成 Nginx map 映射关系,这有利于接下来配置 Nginx 伪静态规则。安装并在站点网络启用此插件,然后在插件设置页面勾上Enable Nginx Map.选项保存,即可看到插件为我们生成的映射关系文件信息。

WordPress 站点网络(多站点|子目录、子域名)Nginx 伪静态设置

二、修改网站配置文件

以宝塔面板为例,在网站配置文件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>

保存完成后应如下图所示:

WordPress 站点网络(多站点|子目录、子域名)Nginx 伪静态设置

三、设置 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以达到性能最大化。


好人卡资源网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress 站点网络(多站点|子目录、子域名)Nginx 伪静态设置
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址