chriszg 发表于 2018-12-25 14:07:18

用Varnish和Memcached缓存给WordPress网站提速-内存级加速

  http://www.freehao123.com/varnish-memcached/
  Varnish是一款高性能的缓存加速器,Varnish把数据存放在服务器的内存中,利用内存可以极大的提高PHP页面执行速度,可以设置0~60秒的精确缓存时间,32位的机器支持的缓存文件最大为2 GB。
  Varnish采用VCL的配置,而且具有强大的管理功能,如top、stat、admin、lis,管理方式比较灵活。Varnish的状态机设计不仅巧妙,结构也很清晰,利用二叉堆管理缓存文件,即可达到随时删除的目的。
  Memcached是一个高性能的分布式内存对象缓存系统,通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached对于减少MysqL数据查询压力非常有帮助。
  由于Varnish采用了Visual Page Cache技术,所有缓存的数据都直接从内存读取,而Squid从硬盘读取缓存的数据,所以Varnish在访问速度方面会更快一些。但是Varnish在高并发状态下,CPU、I/O和内存等资源的开销高于Squid。
  目前Varnish3.0版本解决了服务器重启后Varnish缓存消失的问题,性能优化上有了更大的提升。本篇文章就来分享一下利用Varnish和Memcached缓存来给Wordpress加速,因为要用到内存,所以比较适合那些大内存的服务器。
  如果你正遭遇着大流量给Wordpress服务器带来的压力的苦恼,可以来尝试这些Wordpress提速方法:

[*]  1、Redis加速:用Redis缓存来给WordPress站点加速-适用于Apache和Nginx
[*]  2、负载均衡:免费空间+便宜VPS和OpenResty,Ngx_lua,Redis搭建系统负载均衡环境
[*]  3、CDN加速:DNSPOD分布式解析+安全宝和Incapsula对搜索引擎分别CDN加速
  用Varnish和Memcached缓存给Wordpress网站提速-内存级加速
  一、Varnish安装方法
  1、Varnish官网:

[*]  1、官方网站:https://www.varnish-cache.org/
  2、对于Centos 5的,可以执行以下命令来安装:
rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.el5.centos.noarch.rpm  
yum install varnish
  3、对于是Centos 6的,可以执行以下命令来安装:
rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm  
yum install varnish

  4、如果版本搞错,就会出现如下提示错误:
error: Failed dependencies:  rpmlib(FileDigests)/etc/apt/sources.list
  
apt-get update
  
apt-get install varnish
  6、对于是Ubuntu系统,可以执行以下命令来安装:
curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -  
echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list
  
sudo apt-get update
  
sudo apt-get install varnish
  7、设置Varnish开机启动,执行:chkconfig varnish on和chkconfig varnishncsa on
  8、启动Varnish的命令是:service varnish start和service varnishncsa start

  9、这里还有一个Linux+Nginx+MySQL+PHP+Varnish一键安装包,自动安装完成:Varnish高性能开源HTTP加速器:Varnish Nginx和Varnish Apache搭建配置。
  二、Varnish相关配置
  1、设置好Varnish缓存规则。默认是/etc/varnish/default.vcl,大家可以下载这个Varnish WordPress配置文件覆盖原来的,地址:http://centos.googlecode.com/files/default.vcl。源码内容:
# This is a basic VCL configuration file for varnish.See the vcl(7)  # man page for details on VCL syntax and semantics.
  # Default backend definition.Set this to point to your content
  # server.
  backend default {
  .host = "127.0.0.1";
  .port = "8080";
  }
  acl purge {
  "localhost";
  "127.0.0.1";
  }
  # Below is a commented-out copy of the default VCL logic.If you
  # redefine any of these subroutines, the built-in logic will be
  # appended to your code.
  sub vcl_recv {
  # Only cache the following site
  if (req.http.host ~ "(amhg.freehao123.info)") {
  set req.backend = default;
  } else {
  return (pass);
  }
  if (req.request == "PURGE") {
  if (!client.ip ~ purge) {
  error 405 "Not allowed.";
  }
  return (lookup);
  }
  if (req.restarts == 0) {
  if (req.http.x-forwarded-for) {
  set req.http.X-Forwarded-For =
  req.http.X-Forwarded-For + ", " + client.ip;
  } else {
  set req.http.X-Forwarded-For = client.ip;
  }
  }
  if (req.request != "GET" &&
  req.request != "HEAD" &&
  req.request != "PUT" &&
  req.request != "POST" &&
  req.request != "TRACE" &&
  req.request != "OPTIONS" &&
  req.request != "DELETE") {
  /* Non-RFC2616 or CONNECT which is weird. */
  return (pipe);
  }
  if (req.request != "GET" && req.request != "HEAD") {
  /* We only deal with GET and HEAD by default */
  return (pass);
  }
  if (req.http.Authorization || req.http.Cookie ~ "wordpress_logged" || req.http.Cookie ~ "comment_") {
  /* Not cacheable by default */
  return (pass);
  }
  return (lookup);
  }
  sub vcl_pipe {
  # Note that only the first request to the backend will have
  # X-Forwarded-For set.If you use X-Forwarded-For and want to
  # have it set for all requests, make sure to have:
  # set bereq.http.connection = "close";
  # here.It is not set by default as it might break some broken web
  # applications, like IIS with NTLM authentication.
  return (pipe);
  }
  sub vcl_pass {
  return (pass);
  }
  sub vcl_hash {
  hash_data(req.url);
  if (req.http.host) {
  hash_data(req.http.host);
  } else {
  hash_data(server.ip);
  }
  return (hash);
  }
  sub vcl_hit {
  if (req.request == "PURGE") {
  purge;
  error 200 "Purged.";
  }
  return (deliver);
  }
  sub vcl_miss {
  if (req.request == "PURGE") {
  purge;
  error 200 "Purged.";
  }
  return (fetch);
  }
  sub vcl_fetch {
  if (beresp.ttl
页: [1]
查看完整版本: 用Varnish和Memcached缓存给WordPress网站提速-内存级加速