Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4183434
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: 系统运维

2012-06-21 11:17:04

    前两周我在产品机上部署了一个wordpress站点,结果客户测速发现下载速度只有18kbps,而原站是300kbps.虽然原站是一个纯静态的网站,当然比wordpress的动态网站要快,但是客户还是要求解决这个问题。
    于是乎我在产品机上安装了squid,安装完squid后发现无论如何配置,squid就是不缓存wordpress的页面,只是缓存了图片,js,css之类的静态文件。这个对于网站没有起到任何加速作用,关键还是在动态网页上。
    我仔细对比了缓存的图片和不缓存的页面的http头部后发现,不缓存的页面缺少了Last-Modified这个字段。也就是说得修改wordpress的代码或者安装个插件把Last-Modified加到http头部,我在wordpress.org上找了一遍,没有发现有这样功能的插件。
    于是我花了半天时间研究了wordpress的插件,自己动手写了一个添加Last-Modified的插件(下载见附件)。
    主要代码如下:

点击(此处)折叠或打开

  1. <?php
  2. /*
  3. Plugin Name: Header Last Modified
  4. Plugin URI:
  5. Description: Add post last modified to HTTP header to enable squid or cdn to cache your wordpress web page.
  6.              Squid will not cache the page without http header field Last-Modified.
  7. Version: 1.00
  8. Author: yaofuyuan
  9. Author URI: http://yifangyou.blog.chinaunix.net
  10. */
  11. function header_last_modified() {
  12.     global $post;
  13.     if(isset($post) && isset($post->post_modified)){
  14.         $post_mod_date=date("D, d M Y H:i:s",strtotime($post->post_modified));
  15.         header('Last-Modified: '.$post_mod_date.' GMT');
  16.      }
  17. }
  18. ?>
使用方法:
1.安装后需要在后台启用这个插件,这步是必须的,否则一会会出现500错误
        2.修改/your_website_path/wp-includes/template-loader.php,在 "if ( $template = apply_filters( 'template_include', $template ) )"之前加上header_last_modified();
         例如:

点击(此处)折叠或打开

  1. header_last_modified();
  2.                 if ( $template = apply_filters( 'template_include', $template ) )
  3.                     include( $template );
  4.                 return;

 这个插件的作用是读取当前页的post变量里的修改时间,加入到Http头部里
注意,若是修改的主题文件,这个修改时间是不会变化的




阅读(1770) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~