网站慢就用七牛云加速网站

前几天@Tokin推送了DesNote-Pro 1.09版本,我立即下载进行安装,当我们启用新版主题后,发现gravatar头像和图片都不能显示且都是外链到某个图片地址。所以我就立即查找原因,在functions.php中发现了两处新添加的代码。故有了此篇文章。

网站慢就用七牛云加速网站

网站慢就用七牛云加速网站

正文

正如前言所说,我在functions.php发现了两处新代码,分别是注释为

1.Gravatar头像使用镜像服务器

2.静态资源使用七牛 CDN

本身我前段时间提及的优化就有说,通过用七牛云实现动静态分离来达到加速的效果。刚好此处有两处现成的代码,所以我也赶紧去做。

首先,先观察了两处代码的特征发现,注释为“Gravatar头像使用镜像服务器”的是通过将Gravatar头像缓存至七牛云上的空间使之能够快速加载gravatar头像而不是加载境外网站,根据注释,也就说明是镜像外网。毕竟都知道境外网站大多已被墙,很多朋友也看到了浏览器左下角经常会显示如下图的情况,但是如果使用了这个方法过后,就无需再加载境外网站使wordpress的速度得到提升。

如何使用七牛云对Wordpress进行加速

所以,打算使用这串代码,因为我博客就时常会出现上图的情况。

接下来,我又去看了注释为静态资源使用七牛CDN的代码,看之发现里面出现png|jpg|jpeg|gif|bmp|zip|rar|7z,除了结尾的zip、rar和7z,其他的皆为图片格式,却不见js、css之类的,说明这处的静态资源使用七牛CDN主要是指图片使用七牛云CDN,而js、css都还是依然使用本身的。不过也还是不错,这处代码也被采纳。当然如果想把js、css也弄进来只需在我刚才说的那里添加|css|js,这样写就行


// Gravatar头像使用镜像服务器
function dw_get_avatar($avatar) {
$avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com","secure.gravatar.com"),"image.biji.io",$avatar);
return $avatar;
}
add_filter( 'get_avatar', 'dw_get_avatar', 10, 3 );
// 静态资源使用七牛 CDN
function Qiniu_cdn(){ob_start( 'Qiniu_cdn_replace' );}
add_action( 'template_redirect', 'Qiniu_cdn' );
function Qiniu_cdn_replace( $code ){
    $cdn_exts = 'png|jpg|jpeg|gif|bmp|zip|rar|7z';
    $cdn_dirs = str_replace( '-', '\-', 'wp-content|wp-includes' );
    $regex = '/' . str_replace( '/', '\/', site_url() ) . '\/((' . $cdn_dirs . ')\/[^\s\?\\\'\"\;\>\<]{1,}.(' . $cdn_exts . '))([\"\\\'\s\?]{1})/';
    return preg_replace( $regex, '//file.biji.io/$1$4', $code );
}

而且大家可以看到上方两处代码都有tokin的网址,说明此处需要替换为自己的。但是到底要怎么实现和七牛云的对接呢?我刚好找到404.dog的博客里有介绍使用方法。 需要的朋友可以去看看。我就不写了。

结语

目前我只使用了静态资源使用七牛云CDN加速,Gravatar头像那块还没有弄,因为七牛云对自定义域名的审核速度太慢了,特别是https类的,我现在还在审核当中,等通过后,我也会立即加上的。如果有什么不懂的地方可以问我。

未经允许不得转载:杨永博客 » 网站慢就用七牛云加速网站

赞 (1)

评论 28

  • 昵称 (必填)
  • 邮箱(必填)
  • 网址(选填)
  1. 新用户560865我草,连续两篇了回复
  2. minecraftPiece of writing writing is also a fun, if you be familiar with after that you can write if not it is complicated to write.回复
  3. minecraftAwesome blog! Do you have any recommendations for aspiring writers? I'm planning to start my own site soon but I'm a little lost on everything. Would you advise starting with a free platform like Wordpress or go for a paid option? There are so many choices out there that I'm totally confused .. Any tips? Thank you!回复
  4. minecraftI've been browsing on-line greater than 3 hours as of late, yet I never discovered any fascinating article like yours. It is pretty worth sufficient for me. In my view, if all website owners and bloggers made good content as you did, the web will likely be a lot more useful than ever before.回复
  5. minecraftHello to every body, it's my first go to see of this webpage; this weblog carries remarkable and really good stuff in support of readers.回复
  6. minecraftI enjoy what you guys tend to be up too. This type of clever work and exposure! Keep up the very good works guys I've incorporated you guys to my own blogroll.回复
  7. minecraftDo you have a spam problem on this website; I also am a blogger, and I was wondering your situation; many of us have created some nice practices and we are looking to swap solutions with other folks, be sure to shoot me an email if interested.回复
  8. minecraftThanks for your personal marvelous posting! I definitely enjoyed reading it, you will be a great author. I will be sure to bookmark your blog and will eventually come back very soon. I want to encourage that you continue your great posts, have a nice holiday weekend!回复
  9. minecraft free download 2018Thank you, I've just been searching for info approximately this topic for a long time and yours is the best I've found out so far. However, what about the conclusion? Are you sure in regards to the supply?回复
  10. minecraft free download 2018There is definately a lot to know about this issue. I like all the points you have made.回复
  11. minecraft free download 2018Good post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis. It will always be interesting to read through content from other authors and use something from their websites.回复
  12. minecraft free download 2018naturally like your web-site however you have to test the spelling on several of your posts. A number of them are rife with spelling issues and I to find it very troublesome to tell the reality on the other hand I'll certainly come back again.回复
  13. minecraft free download 2018May I simply just say what a comfort to uncover somebody that truly understands what they are discussing on the internet. You definitely know how to bring an issue to light and make it important. More people must read this and understand this side of the story. I can't believe you are not more popular because you certainly possess the gift.回复
  14. minecraft secret free downloadWrite more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You definitely know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something informative to read?回复
  15. Health Benefits of Coconut OilGood day! I just wish to offer you a huge thumbs up for your great info you have here on this post. I will be returning to your web site for more soon.回复
  16. Health Benefits of Coconut Oilwonderful points altogether, you just received a new reader. What would you suggest in regards to your submit that you just made some days ago? Any positive?回复
  17. Benefits of Coconut OilVery good article. I am dealing with some of these issues as well..回复
  18. Coconut OilKeep this going please, great job!回复
  19. Health Benefits of Coconut OilHowdy! I just wish to offer you a huge thumbs up for the great information you have got right here on this post. I am returning to your blog for more soon.回复
  20. Coconut Oil BenefitsVery nice blog post. I absolutely appreciate this site. Continue the good work!回复
  21. Benefits of Coconut OilMy relatives always say that I am wasting my time here at web, however I know I am getting familiarity daily by reading such good articles or reviews.回复
  22. quest bars cheapWhat's up, just wanted to mention, I enjoyed this article. It was practical. Keep on posting!回复
  23. quest bars cheapHowdy this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding knowledge so I wanted to get advice from someone with experience. Any help would be greatly appreciated!回复
  24. quest bars cheapIt's an remarkable piece of writing in support of all the internet viewers; they will obtain advantage from it I am sure.回复
  25. quest bars cheapGreetings from Florida! I'm bored at work so I decided to check out your website on my iphone during lunch break. I enjoy the knowledge you present here and can't wait to take a look when I get home. I'm shocked at how fast your blog loaded on my cell phone .. I'm not even using WIFI, just 3G .. Anyhow, good blog!回复
  26. quest bars cheapQuest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap I am not sure where you're getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for excellent info I was looking for this info for my mission. Quest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap回复
  27. Quest Protein BarsHmm is anyone else experiencing problems with the images on this blog loading? I'm trying to figure out if its a problem on my end or if it's the blog. Any responses would be greatly appreciated.回复