主页 > 其他 | 服务器技术 > Varnish的简单配置与Drupal的集成

Varnish的简单配置与Drupal的集成

PDF版本

Varnish是非常不错的反向代理以及缓存服务器,高性能网站中Varnish是必不可少的一个环境。此外,用Varnish也可以自建CDN服务等。
关于Varnish的原理以及基础信息,此前有博文参考。

高负载网站之Varnish与Drupal – 基本篇
Varnish构建高负载Drupal网站 – 高级篇

鉴于之前一些朋友咨询Drupal+Varnish+Apache如何配置,因此本篇就介绍一些Varnish+Apache如何简单的配置,给大家演示一下,如何一步一步简单的使用Varnish及Drupal的配合,(注:不是Drupal站点,本文也适用)。

第一步,配置安装配置Varnish

安装Varnish不必说了。
打开Varnish的配置文件 /etc/sysconfig/varnish

修改如下几行:

# VARNISH_LISTEN_ADDRESS=
#Varnsih的端口,暂时不必改,后面会改。
VARNISH_LISTEN_PORT=6081
 
#内存吃紧的服务器,应该使用文件缓存
#数字改大一点
VARNISH_STORAGE_SIZE=2G

接下来,修改/etc/varnish/default.vcl文件。
首先备份该文件,然后清空内容,贴入以下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 = "80";
}
#
# 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 {
  set req.backend = default;
  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;
	  }
  }
  ######################
  # Remove cookies     #
  ######################
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(s_cc|s_sq)=[^;]*", "");
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(base_domain_|fbsetting_)[^;]*", "");
 
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_gscu_|_gscs_|_gscbrs_|zinch_flag_like|zt|paq_cookietime)[^;]*", "");
  set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
 
  if (req.http.Cookie ~ "^\s*$") {
    remove req.http.Cookie;
  }
  # Cache these file types
  if (req.url ~ "\.(jpg|png|css|js|ico|gz|tgz|bz2|tbz|gif)$" && req.url !~ "\?") {
    remove req.http.cookie;
  }
  # Properly handle different encoding types
  if (req.http.Accept-Encoding) {
    if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|csv|pdf)$") {
    # No point in compressing these
      remove req.http.Accept-Encoding;
    } elsif (req.http.Accept-Encoding ~ "gzip") {
      set req.http.Accept-Encoding = "gzip";
    } elsif (req.http.Accept-Encoding ~ "deflate") {
      set req.http.Accept-Encoding = "deflate";
    } else {
      # unknown algorithm
      remove req.http.Accept-Encoding;
    }
  }
 
  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) {
      /* Not cacheable by default */
     return (pass);
  }
  return (lookup);
}
 
sub vcl_fetch {
  if (req.url ~ "\.(jpg|png|css|js|ico|gz|tgz|bz2|tbz|gif)$" && req.url !~ "\?") {
     unset beresp.http.set-cookie;
  }
}

第二步,配置Apache(如果是Ngnix,原理一样)

打开apache的主配置文件,/etc/httpd/conf/httpd.conf文件,找到如下一行

#Listen 127.0.0.1:80
Listen 80

确保监听80端口,绑定所有IP(也就是没有指定绑定的IP),如有修改,请重启Apache。

第三步,测试配置是否成功

启动varnish和apache
在浏览器里面输入你的网址(当前apache配置的网址,或者服务器IP),即可看到你的网站是否正常。(如不正常,请确保apache配置正确!),比如www.abc.com

注意,此刻访问的网站跟varnish没有关系,是直接访问apache服务器的,所以此时一定不能有问题,如有问题也是apache本身配置的问题。

如果一切正常,继续访问6081端口,如www.abc.com:6081,如果访问也正常,那么恭喜,Varnish设置成功了。

第四步,切换端口和修改IP,正式上线!

先到varnish的配置文件/etc/sysconfig/varnish里面,修改如下两个地方,公网IP和端口80.

VARNISH_LISTEN_ADDRESS=[你的公网IP地址]
VARNISH_LISTEN_PORT=80

再到apache的配置文件/etc/httpd/conf/httpd.conf,找到我们之前说的那两行,注释掉下面的,打开上面的,也就是让服务器绑定127.0.0.1,本地回环地址。如下:

Listen 127.0.0.1:80
#Listen 80

重启apache,再重启varnish。

最后,测试访问你的网址(或者IP)。大功告成!
如果要验证varnish是否被使用,可以用firebug打开一个链接查看http头,是否有varnish的标记,如下图所示:

varnish验证标记

varnish使用的http验证标记

后记:Drupal有一个模块,叫Varnish,该模块主要作用是清空缓存之类,提供一个管理界面,本文的设置不需要安装此模块。
当然安装也无妨:P
参见:varnish模块起到什么作用?


声明: 本站所有文章欢迎转载,所有文章未说明,均属于原创,转载均请注明出处。
本文有效链接: http://www.drupal001.com/2014/04/varnish-drupal/
版权所有: Drupal与高性能网站架构 http://www.drupal001.com


,

评论:8

发表评论
  1. avatar
    回复 海阔天空
    14/05/04

    安装3.0.3版的话, req.http.X-Forwarded-For “, ” client.ip;这行是有错误的哟!

    • avatar
      回复 robbin
      14/05/04

      Varnish 2.x 字符串相连是用空格就可以 比如 a = b c,varnish3.x 字符串相连是用加号,+,所以这里会有一些问题。
      改成这样就行:
      set req.http.X-Forwarded-For = req.http.X-Forwarded-For + “, ” + client.ip;

  2. avatar
    回复 rocky
    14/05/12

    请问,在windows 7下如何安装varnish?谢谢

  3. avatar
    回复 robbin
    14/05/12

    varnish在windows下貌似没有,可以试试这个:
    https://www.varnish-cache.org/trac/wiki/VarnishOnCygwinWindows

    • avatar
      回复 rocky
      14/05/12

      谢谢。

  4. avatar
    回复 幽灵
    14/06/03

    同一台服务器的时候,varnish监听了80端口的话,会和Apachech冲突吧?
    我现在的做法是Apache监听8080端口,然后varnish中这样

    backend default {
      .host = "127.0.0.1";
      .port = "8080";
    }
    

    这样有没有什么问题呢?第一次搞varnish……

    • avatar
      回复 robbin
      14/06/03

      你这有也可以。
      两个都监听80端口没有问题,因为监听不同的网卡。
      一个是外网地址,一个是本地回环地址。
      这样的好处就是如果你配置好了虚拟主机,比如
      VirtualHost *:80
      就不用改了。

  5. avatar
    回复 流云
    14/08/04

    楼主可否再开个文章详细介绍下/etc/varnish/default.vcl这个文件里的配置改动说明。
    我去查看http头是有varnish的标识了。
    我的drupal没有启用匿名缓存,我的理解是:有了varnish缓存,如果我改一个地方,并没有清除varnish缓存的话(现在还不知道如何管理清除varnish缓存,我的default.vcl配置都是默认的,只改了端口设置),匿名用户看到的应该是之前存好的varnish缓存,不过事实并非如此,就好像没有varnish缓存一样。
    请楼主解惑,谢谢

rocky 进行回复 取消回复

电子邮件地址不会被公开。 必填项已用 * 标注


5 − = 二

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用:0

下面所列的是引用到本博客的链接
Varnish的简单配置与Drupal的集成 来自 Drupal与高性能网站架构
顶部