• 要嫁就嫁程序猿——钱多话少死的早
  • Google、百度年终盘点出炉,全球网民都在搜索什么?
  • 超赞!人气超高的HTML5特效排行榜TOP 10
  •    7个月前 (03-10)  服务器和软件 |   抢沙发  17 
    文章评分 0 次,平均分 0.0

    第一步:创建七牛公开空间

    首先,你得有一个七牛账号(这不是废话么~)。什么?你才知道七牛?那还不快注册成为七牛用户,立即享受每月 10G 流量 + 10G 存储的免费服务。

    有了账号,你就可以新建空间,我们需要建立一个公开空间。

    非插件实现WordPress站点七牛云存储CDN加速服务

    七牛云存储 - 新建空间

    第二步:设置镜像存储

    成功建立空间之后,打开该空间的【空间设置】页面。

    非插件实现WordPress站点七牛云存储CDN加速服务

    七牛云存储 - 空间设置 在【域名设置】下,我们知道了该空间的默认域名,你也可以绑定自己的域名,这里不展开讨论。

    在【镜像存储】下,点击【一键加速网站】按钮,在弹出的对话框中,【镜像源】填写你的博客地址,确定即可。

    非插件实现WordPress站点七牛云存储CDN加速服务

    七牛云存储 - 加速设置设置加速后,http://alextoo.com/folder/file.jpg 就可以通过 http://alextoo.u.qiniudn.com/folder/file.jpg 来访问了。

    也就是说,我们只需将 WordPress 输出的内容中,所有的 http://alextoo.com/ 域名替换为http://alextoo.u.qiniudn.com/ 就能实现 CDN 加速。(仅作举例,只替换域名是错误的)

    第三步:添加 WordPress 代码

    在 WordPress 主题目录下的 functions.php 文件中,加入以下代码实现替换功能:

    <code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token operator">!</span><span class="token function">is_admin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">add_action</span><span class="token punctuation">(</span><span class="token string">'wp_loaded'</span><span class="token punctuation">,</span><span class="token string">'c7sky_ob_start'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            
            <span class="token keyword">function</span> <span class="token function">c7sky_ob_start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token function">ob_start</span><span class="token punctuation">(</span><span class="token string">'c7sky_qiniu_cdn_replace'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            
            <span class="token keyword">function</span> <span class="token function">c7sky_qiniu_cdn_replace</span><span class="token punctuation">(</span><span class="token variable">$html</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">return</span> <span class="token function">str_replace</span><span class="token punctuation">(</span><span class="token string">'[这里填写博客路径]'</span><span class="token punctuation">,</span> <span class="token string">'[这里填写七牛空间路径]'</span><span class="token punctuation">,</span> <span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    </code>

    以小影志的上传目录 wp-content/uploads/ 文件夹为例,路径就应该替换为如下代码:

    <code class=" language-php"><span class="token keyword">return</span> <span class="token function">str_replace</span><span class="token punctuation">(</span>'http<span class="token punctuation">:</span><span class="token comment" spellcheck="true">//alextoo.com/wp-content/uploads/', 'http://alextoo.u.qiniudn.com/wp-content/uploads/', $html);</span>
    </code>

    如果你需要更具体的范围控制(多目录、指定文件类型),可使用以下代码替换上面的c7sky_qiniu_cdn_replace 部分:

    <code class=" language-php"><span class="token comment" spellcheck="true">//修改自七牛镜像存储 WordPress 插件</span>
    <span class="token keyword">function</span> <span class="token function">c7sky_qiniu_cdn_replace</span><span class="token punctuation">(</span><span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token variable">$local_host</span> <span class="token operator">=</span> 'http<span class="token punctuation">:</span><span class="token comment" spellcheck="true">//alextoo.com'; //博客域名</span>
    <span class="token variable">$qiniu_host</span> <span class="token operator">=</span> 'http<span class="token punctuation">:</span><span class="token comment" spellcheck="true">//alextoo.u.qiniudn.com'; //七牛域名</span>
    <span class="token variable">$cdn_exts</span>   <span class="token operator">=</span> <span class="token string">'js|css|png|jpg|jpeg|gif|ico'</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//扩展名(使用|分隔)</span>
    <span class="token variable">$cdn_dirs</span>   <span class="token operator">=</span> <span class="token string">'wp-content|wp-includes'</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//目录(使用|分隔)</span>
            
    <span class="token variable">$cdn_dirs</span>   <span class="token operator">=</span> <span class="token function">str_replace</span><span class="token punctuation">(</span><span class="token string">'-'</span><span class="token punctuation">,</span> <span class="token string">'\-'</span><span class="token punctuation">,</span> <span class="token variable">$cdn_dirs</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$cdn_dirs</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token variable">$regex</span>        <span class="token operator">=</span>  <span class="token string">'/'</span> <span class="token punctuation">.</span> <span class="token function">str_replace</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token string">'\/'</span><span class="token punctuation">,</span> <span class="token variable">$local_host</span><span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token string">'\/(('</span> <span class="token punctuation">.</span> <span class="token variable">$cdn_dirs</span> <span class="token punctuation">.</span> <span class="token string">')\/[^\s\?\\\'\"\;\&gt;\&lt;]{1,}.('</span> <span class="token punctuation">.</span> <span class="token variable">$cdn_exts</span> <span class="token punctuation">.</span> <span class="token string">'))([\"\\\'\s\?]{1})/'</span><span class="token punctuation">;</span>
    <span class="token variable">$html</span> <span class="token operator">=</span>  <span class="token function">preg_replace</span><span class="token punctuation">(</span><span class="token variable">$regex</span><span class="token punctuation">,</span> <span class="token variable">$qiniu_host</span> <span class="token punctuation">.</span> <span class="token string">'/$1$4'</span><span class="token punctuation">,</span> <span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token variable">$regex</span>        <span class="token operator">=</span> <span class="token string">'/'</span> <span class="token punctuation">.</span> <span class="token function">str_replace</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token string">'\/'</span><span class="token punctuation">,</span> <span class="token variable">$local_host</span><span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token string">'\/([^\s\?\\\'\"\;\&gt;\&lt;]{1,}.('</span> <span class="token punctuation">.</span> <span class="token variable">$cdn_exts</span> <span class="token punctuation">.</span> <span class="token string">'))([\"\\\'\s\?]{1})/'</span><span class="token punctuation">;</span>
                    <span class="token variable">$html</span> <span class="token operator">=</span>  <span class="token function">preg_replace</span><span class="token punctuation">(</span><span class="token variable">$regex</span><span class="token punctuation">,</span> <span class="token variable">$qiniu_host</span> <span class="token punctuation">.</span> <span class="token string">'/$1$3'</span><span class="token punctuation">,</span> <span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> <span class="token variable">$html</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    </code>

    大功告成

    简单的3步,就能让你的 WordPress 实现 CDN 加速。当然,如果你的需求还包括加速远程图片或刷新缓存等,那么还是老老实实安装七牛镜像存储 WordPress 插件吧。非插件实现WordPress站点七牛云存储CDN加速服务

     

    除特别注明外,本站所有文章均为小杜博客原创,转载请注明出处来自http://www.alextoo.com/854.html

    关于
    网络推广,网页设计,网站搭设,主机搭设。QQ:348960279

    发表评论

    表情 格式

    暂无评论

    登录

    忘记密码 ?

    您也可以使用第三方帐号快捷登录

    切换登录

    注册

    扫一扫二维码分享