解决wordpress 404错误引起的网站cpu超负

作者:笑望书 发布时间:December 30, 2011 分类:建站心得

发觉最近我的网站真的是多灾多难,上午才把一个discuz的网站cpu超负的问题解决了(见原文插件有风险,安装需谨慎—记discuz x2 cpu超负),现在又轮到一个wordpress的网站cpu超负(两个网站使用的是不同空间),莫非2012年真的是世界末日?

虽然到现在还是没有找到完美的解决方法,但是还是暂时解决了cpu超负载的问题。下面是我具体解决方法。

首先排除插件的原因(没办法上次discuz超负教会了我不要忽略插件的影响),其后排除空间的原因,接着就是下载网站日志进行分析(推荐分析日志的工具web log explorer),发现很多404错误,贴个图看看

网站日志

很奇怪,为什么我的网站会有这些页面的错误,什么asp页面、plus目录下的文件,我从来没用这个域名装过这些,更奇的是还有rar、zip文件的下载404错误,我压根没放过rar zip文件,到现在我都没想明白这些文件是怎么指向我的域名的。

先不想想不明白的事,先解决问题才是硬道理。早就听说wordpress的404错误会严重消耗空间资源,所以我的问题90%机会是这个原因,于是去网上搜索wordpress 404错误消耗资源的相关文章,可惜没有找到完美的解决方法。最后综合了下面两篇文章介绍的方法,
http://newphp.net/contents/147/
http://www.chinaz.com/web/2011/0417/172783.shtml

通过修改.htaccess对rewriterule进行重写,将网页大多数404错误定到一个静态的404.shtml页面。我那个wp网站开启了二级域名多站点功能,网站原来.htaccess的内容如下
#www.yangwo.net测试
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]

修改后的.htaccess内容

#404页
ErrorDocument 404 /404.shtml

RewriteEngine On
# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(css|js|txt|mp3|zip|exe|rmvb|rm|avi)$

RewriteRule ^post/.*$ /index.php [L]
RewriteRule ^videos/.*$ /index.php [L]
RewriteRule ^archives/.*$ /index.php [L]
RewriteRule ^mbox/*$ /index.php [L]
RewriteRule ^bulanting/*$ /index.php [L]
RewriteRule ^about.*$ /index.php [L]
RewriteRule ^feed.*$ /index.php [L]
RewriteRule ^page/.*$ /index.php [L]

RewriteRule ^zuocai.*$ /index.php [L]
RewriteRule ^chuangyi.*$ /index.php [L]
RewriteRule ^anime.*$ /index.php [L]
RewriteRule ^ads.*$ /index.php [L]
RewriteRule ^egao.*$ /index.php [L]
RewriteRule ^gaoxiao.*$ /index.php [L]
RewriteRule ^stars.*$ /index.php [L]
RewriteRule ^liuxingmen.*$ /index.php [L]
RewriteRule ^games.*$ /index.php [L]
RewriteRule ^film-tv.*$ /index.php [L]
RewriteRule ^jingcai.*$ /index.php [L]
RewriteRule ^leiren.*$ /index.php [L]
RewriteRule ^music.*$ /index.php [L]
RewriteRule ^tougao.*$ /index.php [L]
RewriteRule ^kepengdami.*$ /index.php [L]
RewriteRule ^youqushipin.*$ /index.php [L]
RewriteRule ^quwen.*$ /index.php [L]
# END WordPress

我那个网站把分类链接的cotegory用插件去掉了,从上面的规则可以看到每个分类和页面都要写一个规则,以后添加新分类或新页面都要在里面重新加一条规则,想想真是不爽,但是现在也没有好的办法,当初wordpress固定链接的设置加个前缀就好了。

修改后cpu迅速恢复正常。

2012-01-02更新
把原来的wordpress缓存插件DB-cache-reloaded换成Hyer Cache,果然网页打开快好多!

标签: wordpress, 网站cpu超负

添加新评论 »