🦄 2024 独立开发者训练营,一起创业!查看介绍 / 立即报名 →

创建高性能 Drupal 应用:使用 Varnish

Varnish,是一种加速器,可以提升网站的性能。 Drupal 网站经常会用到它,比如 Drupal.org 就使用了 Varnish。还有个著名的视频网站 Vimeo.com(国内无法访问)也用了 Varnish。在这个配置手册里,我们会在 CentOS 服务器上安装并配置一下 Varnish ,并且让它可以跟 Drupal 结合在一起使用。

一、安装 Varnish

得先去给系统添加一个资源库,由 Varnish 官方提供,这样我们才能安装新版的 Varnish,根据自己的系统版本,执行下面的命令:

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

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 命令,查看一下要安装的 Varnish 相关信息:

yum info varnish

返回:

Available Packages
Name : varnish
Arch : x86_64
Version : 3.0.5
Release : 1.el6
Size : 444 k
Repo : varnish-3.0
Summary : High-performance HTTP accelerator
URL : http://www.varnish-cache.org/
License : BSD
Description : This is Varnish Cache, a high-performance HTTP accelerator.
: Documentation wiki and additional information about Varnish is
: available on the following web site: http://www.varnish-cache.org/

注意 Version 后面的信息是 3.0.5,这是目前最新的稳定版本。确定以后,就可以使用 yum 安装 varnish 了:

yum install varnish

二、配置 Varnish

下面,我们得去配置一下 Varnish,让它可以跟 nginx 或者 apache 服务器配合在一起工作。可以修改两个地方,先去编辑一下 /etc/sysconfig/varnish ,使用 vim 命令:

vim /etc/sysconfig/varnish

找到:

VARNISH_LISTEN_PORT=6081

换成:

VARNISH_LISTEN_PORT=80

这里我们修改了 Varnish 的监听的端口号,改成了 80 端口,也就是用户访问我们的网站,首先是由 Varnish 来接待他们,后端程序的处理,可以交给 nginx 或者 apache ,所以我们需要告诉 Varnish 后端处理在哪里,去编辑一下 /etc/varnish/default.vcl

vim /etc/varnish/default.vcl

找到:

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

换成:

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

注意我们修改了默认后端处理程序的端口号,把 80 端口修改成了 8080,然后需要再去修改一下 apache 或者 nginx 服务监听的端口号为这个端口号。

三、配置 Web 服务

现在,我们把 Varnish 放在 Web 应用的前面去应对匿名用户请求的静态文件,然后告诉 Varnish 后端让谁去处理。现在,我们得去配置一下后端的 Web 服务,比如 nginx 或 apache,告诉它监听的端口,也就是前面我们告诉  Varnish 的 8080 端口。这里我用的是 nginx 服务器,配置文件是在:/etc/nginx/conf.d ,你可能已经创建了一个自己的 nginx 配置文件,那么就去修改这个配置文件。另外这个目录下可能会有 default.conf,你需要把它删除掉,或者放在别处,不然你也需要去修改这个默认的配置文件。用 vim 命令打开你需要的 nginx 配置文件。

找到:

 listen      80;

换成:

 listen      8080;

现在用户请求访问 Web 应用页面的时候,先由 Varnish 去处理,如果需要处理程序提取内容的时候,就把请求再交给 nginx 去处理。

重启 nginx 还有 varnish 服务:

service nginx restart
service varnish restart

四、为 Drupal 安装  Varnish 模块

Drupal 的 Varnish 模块可以让 Varnish 跟 Drupal 配合在一起。这里我们为 Drupal 安装 7.x-1.x-dev(因为比较新) 版本的 Varnish 模块。安装好以后可以打开配置的界面:admin/config/development/varnish

选择 Varnish 的版本,选择 3.x。再输入 Varnish Control Key,是一串数字,在服务器上可以得到,地址是: /etc/varnish/secret,完成以后,点击 保存设置。成功以后,在这个页面下方,会显示 Varnish running

配置 settings.php

编辑 sites/default/settings.php,添加下面这几行代码:

$conf['cache_backends'][] = 'sites/all/modules/varnish/varnish.cache.inc';
$conf['page_cache_invoke_hooks'] = FALSE;
$conf['cache_class_cache_page'] = 'VarnishCache';

找到:

# $conf['reverse_proxy'] = TRUE;

换成(取消了注释用的 # 号):

$conf['reverse_proxy'] = TRUE;

找到:

# $conf['reverse_proxy_addresses'] = array('a.b.c.d', ...);

换成:

$conf['reverse_proxy_addresses'] = array('127.0.0.1');

保存 Drupal 的配置文件。开启 Drupal 网站的缓存功能( admin/config/development/performance ),然后打开 Is Varnish working? 去测试一下我们配置的 Varnish 是否生效了。输入网站的地址点击 Check it! ,如果返回的页面上显示 Yes,说明成功了。

varnish-check

Drupal 服务器 Varnish

评论

足够大内存服务器或多台服务器才会有比较好的作用. 反正用了一下就卸掉了, 很费内存, 内存不多的话, 写入和删除缓存的周期会比较短, 性能反而降低, 直观感觉斯这样..........

好吧,nginx+Apache+varnish我还是没配置成功。。

哪里出了问题。

服务也运行成功了 就是无法成功使用。。。应该是端口问题

我nginx用的80
Apache用的8080
Apache处理php通过nginx的poxy代理把前端交给nginx进行处理

然后varnish怎么都对接不上 愁死人

$conf['reverse_proxy_addresses'] = array('127.0.0.1');
我一开始这个,网站就无法访问,变空白页。

是根据步骤一步一步来的吗?你能正确的配置 Varnish 服务吗?也就是你安装完 Varnish 以后,并且做了相应的配置,能否让网站正常打开。在修改 settings.php 的时候,有没有先取消 $conf['reverse_proxy'] = TRUE; 前面的注释。

这个问题我解决了,是我后面写多了几个点,可是我测试的时候不行:
Yes!
Sort of!

Varnish appears to be responding at that url, but the Cache-Control header's "max-age" value is less than 1, which means that Varnish will never serve content from cache at this url.

The max-age value appears to be: 0

This may be intentional, but if you want Varnish to cache this url you'll have to fix the max-age value the application is sending to Varnish.

And here, for you to ponder further, are the actual headers we received:

我在用 Nginx 服务器测试的时候,如果出现这个提示,再次去检查一下就可以了。

哦,应该是没有开启 Drupal 的缓存功能( admin/config/development/performance ),勾选一下 对匿名用户缓存页面 。

像这种技术文章应该要收取比特币支付才能查看,以支持博主,和网站开销。

呵呵。比特币是啥东西呀。

这种最佳实践的好文居然免费开放,实在太高大上了。

嘿嘿,忍不住

博主你好,我是从头开始按照你的步骤安装的,安到给drupal安装模块那步后,去我的网站看了下,结果访问不了了... http://www.drupalba.com/
下面的都还没做呢,请问我现在这种情况该怎么办呢?我安varnish的时候好像都很正常,成功了。

后台能进吗。或者直接在目录里删除安装的 varnish 模块试试。

额... 问题就是我还没弄模块这些东西呢。网站已经访问不了了。
是不是顺序要反过来啊,先装模块然后设置模块,再去服务器安装varnish服务呢?

幸好我本地有备份,我在本地安了varnish模块设置了下,然后把数据库覆盖上去了。
不过还是不行,我看有个Varnish Control Terminal *选项,默认是127.0.0.1:6082,默认我看不行,我就改成了127.0.0.1:80,也不行。不知这个具体应该改成什么。
反正现在还是老样子...

好了.. 重新设置了下apache的设置,我想不起来当时怎么弄错的了

varnish 可以 和 memcache 一起使用吗?

可以

请问isvarnishworking检测结果是这样应该怎么解决呢。
Yes!
Sort of!
Varnish appears to be responding at that url, but the "Age" header is less than 1.

This means that either, 1) you checked right when Varnish cleared it's cache for that url, or 2) for whatever reason Varnish is not actually serving the content for that url from cache.

If it is the former, just check again and see if you get a more solid confirmation.

If it is the latter, perhaps one of the following is happening:

请问楼主,drupal的那个配置修改,是说把drupal的页面缓存用varnish缓存来替代是嘛?因为我看cache_page表里没有数据了!但是我改了一些设置比如站点名字后,去用匿名访问看却是最新修改后的内容,也就是不需要清除缓存,好像没有启用缓存似得... 不知道安装varnish模块的意义何在?

请好,我在安装使用的过程中也遇到问题,想请教博主,希望有解。
我的环境,mac + apache
安装varnish 的过程(使用了port安装):
https://www.lullabot.com/blog/article/installing-varnish-mac-os-x-mamp-o...
安装:sudo port install varnish
最终:配制文件路径在:/opt/local/etc/varnish/default.vcl,/opt/local/etc/varnish/varnish.conf
varnishd文件在:/opt/local/sbin/varnishd
==================

为什么评论只能显示一行?

请好,我在安装使用的过程中也遇到问题,想请教博主,希望有解。

我的环境,mac + apache 安装varnish 的过程(使用了port安装):https://www.lullabot.com/blog/article/installing-varnish-mac-os-x-mamp-o...

强烈建议使用 vagrant 去搭建开发环境,放弃 mamp 等一键安装的环境。

安装:sudo port install varnish 最终:配制文件路径在:/opt/local/etc/varnish/default.vcl,/opt/local/etc/varnish/varnish.conf varnishd文件在:/opt/local/sbin/varnishd

apache情况:网站www.sitea.com 在apache 的vhost 中绑定了端口8080,也就是说 www.sitea.com:8080可以正常访问到网站,在firebug中看到的header信息不包含“X-Varnish”

varnish 配制情况 :/opt/local/etc/varnish/default.vcl中backend default { .host = "127.0.0.1"; .port = "8080";} Control Key,这个没有值 ,在安装的varnish 中也没有发现博主说的secret文件,故这项为空settings.php文件添加了和修改了配制如此文。Varnish Control Terminal :这里填写:127.0.0.1: .访问网站www.sitea.com 在header信息中有一个“X-Varnish 1771625176”,这意味着varnish配制成功了么?2.为什么在drupal后台配制varnish的页面返回的消息是:The Varnish control terminal is not responding at 127.0.0.1 on port 8080.3.为什么没有生成 Varnish Control Key 的secret文件。谢谢……

请好,我在安装使用的过程中也遇到问题,想请教博主,希望有解。

按照blog写的配置完了,运行了有30分钟,然后出现此错误。

centos+nginx+drupal7

Error 503 Service Unavailable

Service Unavailable

Guru Meditation:

XID: 1008073093

Varnish cache server

重启以后,每隔 30 分钟都会这样吗?

皓哥 请问 Boost + nginx 怎么配置? 搞了好久没有结果 求教
.htaccess 不知道怎么配置

请问皓哥!你的网站有用这个配置。我的才1核 1G内存 !这样跑起来很慢。请问皓哥用的是什么配置!

baitongda ,我用的是 8核 16G 的内存。

在vagrant环境配置完了。出现了 Error 503 Service Unavailable

Service Unavailable

Guru Meditation:

XID: 1449165190

Varnish cache server

谢谢!好了!我安装的是开发版的模块才好了。前面装的稳定版总是提示错误。

嗯,下次遇到问题记得来宁皓社区:http://talk.ninghao.net

按照博主方法配置以前视乎成功了,今天服务器安装gitlab时出现了小问题,导致站点feaplus.com不能访问了,删除gitlab之后,重启所有服务后,现在已经可以正常访问了.但是明显比以前慢了,使用Is Varnish Working?检查varnish,出现如下内容,是否varnish出问题了,出在了哪里呢?

We didn't find an "Age" header, which is generally what you look for to see if Varnish is actually serving the content from cache.
We did find the "X-Varnish" header however, so Varnish appears to be technically responding at that url, but we can't say for sure if it is actually serving that content from it's cache.

微信好友

用微信扫描二维码,
加我好友。

微信公众号

用微信扫描二维码,
订阅宁皓网公众号。

240746680

用 QQ 扫描二维码,
加入宁皓网 QQ 群。

统计

14696
分钟
0
你学会了
0%
完成

社会化网络

关于

微信订阅号

扫描微信二维码关注宁皓网,每天进步一点